How can I test whether a string contains another string in fish shell? - fish

How do I test for the presence of a substring in fish shell? For instance, within a switch expression:
set myvar "a long test string"
switch $myvar
case magical-operator-here "test string"
echo 'yep!'
case '*'
echo 'nope!'
end

The * is the wildcard character, so
set myvar "a long test string"
switch $myvar
case "*test string"
echo 'yep!'
case '*'
echo 'nope!'
end
If you wish to test if it ends with that string. If it can also appear somewhere in the middle, add another * at the end.
Also, since 2.3.0, fish has had a string builtin with a match subcommand, so you could also use string match -q -- "*test string" $myvar. It also supports pcre-style regexes with the "-r" option.

Related

Finding a string between two ranges of strings or end of string

I am trying to extract whatever is between two strings. The first string is a known string, the second string could be from a list of strings.
For example,
We have the start string and the end strings. We want to get the text between these.
start = "start"
end = ["then", "stop", "other"]
Criteria
test = "start a task then do something else"
result = "a task"
test = "start a task stop doing something else"
result = "a task"
test = "start a task then stop"
result = "a task"
test = "start a task"
result = "a task"
I have looked at using a regex, and I got one which works for between two strings, I just cannot create one which words with a option of strings:
(?<=start\s).*(?=\sthen)
I have tried using this:
(?<=start\s).*(?=\sthen|\sstop|\sother)
but this will include 'then, stop or other' in the match like so:
"start a task then stop" will return "a task then"
I have also tried to do a 'match any character except the end list" in the capture group like so: (?<=start\s)((?!then|stop|other).*)(?=\sthen|\sstop|\sother) but this has the same effect as the one above.
I am using swift, so I am also wondering whether this can be achieved by finding the substring between two strings.
Thanks for any help!
You may use
(?<=start\s).*?(?=\s+(?:then|stop|other)|$)
See the regex demo. To search for whole words, add \b word boundary in proper places:
(?<=\bstart\s).*?(?=\s+(?:then|stop|other)\b|$)
See another regex demo
Details
(?<=start\s) - a positive lookbehind that matches a location immediately preceded with start string and a whitespace
.*? - any 0+ chars other than line break chars, as few as possible
(?=\s+(?:then|stop|other)|$) - a position in the string that is immediately followed with
\s+ - 1+ whitespaces
(?:then|stop|other) - one of the words
|$ - or end of string.

Autohotkey: How to add apostrophe (") to string

I have a autohotkeyscript with:
IfInString, pp_text, %A_Space%
{
pp_text := %pp_text%
}
So in case %pp_text% contains a space I want to add " at the beginning and end
Example:
pp_text = Hello World
should then become
pp_text = "Hello World"
How can I do this
You escape a quote by by putting another quote next to it and you concatenate with the concatenate operator ., but actually you can also just omit the operator 99% of the time.
Other things to fix in your script:
Get rid of that super deprecated legacy command and use InStr() instead.
And when you're in an expression, you refer to a variable by just typing its name. Those double % you're using are the legacy way of referring to a variable.
So it's correct in the legacy command, but not in the modern := assignment.
And also you can omit brackets on one-liner if-statements. But that's of course just going to be down personal preference.
Full script:
If (InStr(pp_text, A_Space))
pp_text := """" pp_text """"
Four quotes since the the outer two specify that we're typing a string.
Variable names in an expression are not enclosed in percent signs.
Consequently, literal strings must be enclosed in double quotes to
distinguish them from variables.
To include an actual quote-character
inside a literal string, specify two consecutive quotes as shown twice
in this example: "She said, ""An apple a day.""".
pp_text := "Hello World"
If InStr(pp_text, " ")
pp_text := """Hello World"""
MsgBox % pp_text
EDIT:
To use the name of the variable (not its literal text) in the output expression, you need four quotes, as the user 0x464e explained.
pp_text := "Hello World"
If InStr(pp_text, " ")
pp_text := """" pp_text """"
MsgBox % pp_text

Pass string value having space to another powershell file

I have a powershell script which calls another powershell file passing a string argument.
param (
[string]$strVal = "Hello World"
)
$params = #{
message = "$strVal"
}
$sb = [scriptblock]::create(".{$(get-content $ps1file -Raw)} $(&{$args} #params)")
Somehow the script passes message variable without double quotes so the powershell file receives only the first part of the message variable (before space) i.e. "Hello".
How can I pass the strVal variable with space (i.e. "Hello World")?
A double quote pair signals PowerShell to perform string expansion. If you want double quotes to output literally, you need to escape them (prevent expansion) or surround them with single quotes. However, a single quote pair signals PowerShell to treat everything inside literally so your variables will not interpolate. Given the situation, you want string expansion, variable interpolation, and literal double quotes.
You can do the following:
# Double Double Quote Escape
#{message = """$strVal"""}
Name Value
---- -----
message "Hello World"
# Backtick Escape
#{message = "`"$strVal`""}
Name Value
---- -----
message "Hello World"
Quote resolution works from left to right, which means the leftmost quote type takes precedence. So '"$strVal"' will just print everything literally due to the outside single quote pair. "'$strVal'" will print single quotes and the value of $strVal.

Implement Scala-style String Interpolation In Scala

I want to implement a Scala-style string interpolation in Scala. Here is an example,
val str = "hello ${var1} world ${var2}"
At runtime I want to replace "${var1}" and "${var2}" with some runtime strings. However, when trying to use Regex.replaceAllIn(target: CharSequence, replacer: (Match) ⇒ String), I ran into the following problem:
import scala.util.matching.Regex
val placeholder = new Regex("""(\$\{\w+\})""")
placeholder.replaceAllIn(str, m => s"A${m.matched}B")
java.lang.IllegalArgumentException: No group with name {var1}
at java.util.regex.Matcher.appendReplacement(Matcher.java:800)
at scala.util.matching.Regex$Replacement$class.replace(Regex.scala:722)
at scala.util.matching.Regex$MatchIterator$$anon$1.replace(Regex.scala:700)
at scala.util.matching.Regex$$anonfun$replaceAllIn$1.apply(Regex.scala:410)
at scala.util.matching.Regex$$anonfun$replaceAllIn$1.apply(Regex.scala:410)
at scala.collection.Iterator$class.foreach(Iterator.scala:743)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1174)
at scala.util.matching.Regex.replaceAllIn(Regex.scala:410)
... 32 elided
However, when I removed '$' from the regular expression, it worked:
val placeholder = new Regex("""(\{\w+\})""")
placeholder.replaceAllIn(str, m => s"A${m.matched}B")
res2: String = hello $A{var1}B world $A{var2}B
So my question is that whether this is a bug in Scala Regex. And if so, are there other elegant ways to achieve the same goal (other than brutal force replaceAllLiterally on all placeholders)?
$ is a treated specially in the replacement string. This is described in the documentation of replaceAllIn:
In the replacement String, a dollar sign ($) followed by a number will be interpreted as a reference to a group in the matched pattern, with numbers 1 through 9 corresponding to the first nine groups, and 0 standing for the whole match. Any other character is an error. The backslash (\) character will be interpreted as an escape character and can be used to escape the dollar sign. Use Regex.quoteReplacement to escape these characters.
(Actually, that doesn't mention named group references, so I guess it's only sort of documented.)
Anyway, the takeaway here is that you need to escape the $ characters in the replacement string if you don't want them to be treated as references.
new scala.util.matching.Regex("""(\$\{\w+\})""")
.replaceAllIn("hello ${var1} world ${var2}", m => s"A\\${m.matched}B")
// "hello A${var1}B world A${var2}B"
It's hard to tell what you're expecting the behavior to do. The issue is that s"${m.matched}" is turning into "${var1}" (and "${var2}"). The '$' is special character to say "place the group with name {var1} here instead".
For example:
scala> placeholder.replaceAllIn(str, m => "$1")
res0: String = hello ${var1} world ${var2}
It replaces the match with the first capturing group (which is m itself).
It's hard to tell exactly what you're doing, but you could escape any $ like so:
scala> placeholder.replaceAllIn(str, m => s"${m.matched.replace("$","\\$")}")
res1: String = hello ${var1} world ${var2}
If what you really want to do is evaluate var1/var2 for some variables in the local scope of the method; that's not possible. In fact, the s"Hello, $name" pattern is actually converted into new StringContext("Hello, ", "").s(name) at compile time.

Scala string pattern matching for mathematical symbols

I have the following code:
val z: String = tree.symbol.toString
z match {
case "method +" | "method -" | "method *" | "method ==" =>
println("no special op")
false
case "method /" | "method %" =>
println("we have the special div operation")
true
case _ =>
false
}
Is it possible to create a match for the primitive operations in Scala:
"method *".matches("(method) (+-*==)")
I know that the (+-*) signs are used as quantifiers. Is there a way to match them anyway?
Thanks from a avidly Scala scholar!
Sure.
val z: String = tree.symbol.toString
val noSpecialOp = "method (?:[-+*]|==)".r
val divOp = "method [/%]".r
z match {
case noSpecialOp() =>
println("no special op")
false
case divOp() =>
println("we have the special div operation")
true
case _ =>
false
}
Things to consider:
I choose to match against single characters using [abc] instead of (?:a|b|c).
Note that - has to be the first character when using [], or it will be interpreted as a range. Likewise, ^ cannot be the first character inside [], or it will be interpreted as negation.
I'm using (?:...) instead of (...) because I don't want to extract the contents. If I did want to extract the contents -- so I'd know what was the operator, for instance, then I'd use (...). However, I'd also have to change the matching to receive the extracted content, or it would fail the match.
It is important not to forget () on the matches -- like divOp(). If you forget them, a simple assignment is made (and Scala will complain about unreachable code).
And, as I said, if you are extracting something, then you need something inside those parenthesis. For instance, "method ([%/])".r would match divOp(op), but not divOp().
Much the same as in Java. To escape a character in a regular expression, you prefix the character with \. However, backslash is also the escape character in standard Java/Scala strings, so to pass it through to the regular expression processing you must again prefix it with a backslash. You end up with something like:
scala> "+".matches("\\+")
res1 : Boolean = true
As James Iry points out in the comment below, Scala also has support for 'raw strings', enclosed in three quotation marks: """Raw string in which I don't need to escape things like \!""" This allows you to avoid the second level of escaping, that imposed by Java/Scala strings. Note that you still need to escape any characters that are treated as special by the regular expression parser:
scala> "+".matches("""\+""")
res1 : Boolean = true
Escaping characters in Strings works like in Java.
If you have larger Strings which need a lot of escaping, consider Scala's """.
E. g. """String without needing to escape anything \n \d"""
If you put three """ around your regular expression you don't need to escape anything anymore.