I have an issue facing me on a string in Flutter, specifically in a URL that includes '$' char.
Is there any way to escape the dollar sign $?
var url = Uri.parse('https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/ExpectativaMercadoMensais?$top=100&$skip=0&$orderby=Data%20desc&$format=json&$select=Indicador,DataReferencia,Mediana,baseCalculo');
Dart has something called String Interpolation. Here's a snippet from the docs:
To put the value of an expression inside a string, use ${expression}. If the expression is an identifier, you can omit the {}.
Here are some examples of using string interpolation:
String
Result
'${3 + 2}'
'5'
'${"word".toUpperCase()}'
'WORD'
'$myObject'
The value of myObject.toString()
In your case, the URL string contains exactly the escape symbol $ to make a String interpolation. Dart thinks all words after the $ symbol are identifiers (variables, functions etc.) but it doesn't find them defined anywhere. To fix it just do what #jamesdlin suggested: Escape the $ symbols like \$ or prefix the string with r like below:
r'https://...Mensais?$top=100&$skip=0&$orderby=...'.
Use this instead
var url = Uri.parse( 'https://olinda.bcb.gov.br/olinda/servico/Expectativas/versao/v1/odata/ExpectativaMercadoMensais?\$top=100&\$skip=0&\$orderby=Data%20desc&\$format=json&\$select=Indicador,DataReferencia,Mediana,baseCalculo');
Use a reverse slash to escape string
Related
In the example above, I have a String? value of dictionary key containing unescaped backslashes. The assertion fails because the literal string I defined for comparison is escaped.
How do I define the string literal so it is unescaped in the comparison?
Or should I be escaping the dictionary value?
For example, in Perl I would define the string using single quotes or q().
I saw that there is some sort of RFC for a similar Swift solution, but what is the workaround?
Your comparison will work if you use only one method, not both of them: either escape the characters (but do not use #...#). OR if you use #...#, don't escape the characters.
I.e. for a string X4\\7, both of the following will pass the comparison correctly:
"X4\\\\7"
#"X4\\7"#
The following match returns false. How can I change the regular expression to correct it?
"hello$world" -match '^hello$(wo|ab).*$'
"hello$abcde" -match '^hello$(wo|ab).*$'
'hello$world' -match '^hello\$(wo|ab).*$'
'hello$abcde' -match '^hello\$(wo|ab).*$'
You need to quote the left hand side with single quotes so $world isn't treated like variable interpolation. You need to escape the $ in the right hand side so it isn't treated as end of line.
From About Quoting Rules:
When you enclose a string in double quotation marks (a double-quoted string), variable names that are preceded by a dollar sign ($) are replaced with the variable's value before the string is passed to the command for processing.
...
When you enclose a string in single-quotation marks (a single-quoted string), the string is passed to the command exactly as you type it. No substitution is performed.
From About Regular Expressions:
The two commonly used anchors are ^ and $. The carat ^ matches the start of a string, and $, which matches the end of a string. This allows you to match your text at a specific position while also discarding unwanted characters.
...
Escaping characters
The backslash \ is used to escape characters so they are not parsed by the regular expression engine.
The following characters are reserved: []().\^$|?*+{}.
You will need to escape these characters in your patterns to match them in your input strings.
I want replace a Letter with a literal $. I tried:
var s = string.replaceAll("Register","$10")
I want that this text Register saved to be changed to: $10 saved
Illegal group reference is the error I get.
If you look at the scaladoc for replaceAll, you'll see that it takes a regular expression string as the parameter. Escape the $ with a \, or use replaceAllLiterally
replaceAll uses a regular expressions to find the match. In the replacement string $ is a special character that refers to a specific capture group in the matching string. You have no capture groups so this is an error. It's not what you want anyway since you want the literal text "$10".
Usereplaceinstead ofreplaceAll`. It just does a direct string replacement.
I have tried to print it but it just by passes because it's an escaped character.
e.g output should be as follows.
\correct
For that and also future reference:
\0 – Null character (that is a zero after the slash)
\\ – Backslash itself. Since the backslash is used to escape other characters, it needs a special escape to actually print itself.
\t – Horizontal tab
\n – Line Feed
\r – Carriage Return
\” – Double quote. Since the quotes denote a String literal, this is necessary if you actually want to print one.
\’ – Single Quote. Similar reason to above.
Use the following code for Swift 5, Xcode 10.2
let myText = #"This is a Backslash: \"#
print(myText)
Output:
This is a Backslash: \
Now not required to add a double slash to use a single slash in swift 5, even now required slash before some character, for example, single quote, double quote etc.
See this post for latest update about swift 5
https://www.hackingwithswift.com/articles/126/whats-new-in-swift-5-0
var s1: String = "I love my "
let s2: String = "country"
s1 += "\"\(s2)\""
print(s1)
It will print I love my "country"
The backslash character \ acts as an escape character when used in a string. This means you can use, for example, double quotes, in a string by pre-pending them with \. The same also applies for the backslash character itself, which is to say that println("\\") will result in just \ being printed.
This does not work:
scala> """one\r\ntwo\r\nthree\r\nfour""".replace("\r\n", "\n")
res1: String = one\r\ntwo\r\nthree\r\nfour
How to do that in Scala?
Is there a more idiomatic way of doing that, instead of using replace?
The problem is that """ quotes does not expand escape sequences. Three different approaches:
Use single " quotes in order to treat escape sequences correctly: "one\r\ntwo";
Use the s string interpolator, be careful following this approach cause this could lead to unexpected replacements: s"""one\r\ntwo""";
Call treatEscapes directly to expands escape sequences in your string: StringContext.treatEscapes("""one\r\ntwo""").
Refer also to this earlier question.
try this
"""one\r\ntwo\r\nthree\r\nfour""".replace("\\r\\n", "\n")
\ is treated as escape charater within string, so you need to tell the compiler that its not a escape character but a string.