help?> @raw_str
@raw_str -> String
Create a raw string without interpolation and unescaping. The exception is that quotation marks still must be escaped. Backslashes escape both quotation marks and other
backslashes, but only when a sequence of backslashes precedes a quote character. Thus, 2n backslashes followed by a quote encodes n backslashes and the end of the
literal while 2n+1 backslashes followed by a quote encodes n backslashes followed by a quote character.
Examples
≡≡≡≡≡≡≡≡≡≡
julia> println(raw"\ $x")
\ $x
julia> println(raw"\"")
"
julia> println(raw"\\\"")
\"
julia> println(raw"\\x \\\"")
\\x \"
raw"\t" already works. You only run into trouble for backslashes at the end of the string, since it is ambiguous whether they are intended as a backslash or as an escape of the final ". In any case, you can always just write an ordinary string literal with backslash escapes: e.g. raw"\t" is equivalent to "\\t".
You never need the raw string syntax — it is just convenient sugar for strings like raw"C:\foo\bar\baz" where you would otherwise need lots of extra backslashes.
You wrote: In Julia 0.6, some strings are impossible to write as raw"…" strings" .
This is true in Julia 0.7 too…
It could be disputable. Problem described in SO seems to be still solvable only by using hack.
But you are probably right because it is more problem of SubstitutionString than raw string. Although possibility to escape chars in raw string syntax would help too. (I don’t propose it now)
See:
# I am trying to create MWE here.
# We want to add \t to found group in replace
# So "a\t" has to change to "a\t\t"
# (or "b\t" to "b\t\t" )
julia> grp = r"([a-z]+\t)"
julia> replace("a\t", grp => "\1\t") # this is NOK
"\x01\t"
julia> replace("a\t", grp => "\1\t") # this is NOK
"\\1\t"
# next two examples show where we NEED escape \t
julia> replace("a\t", grp => s"\1\\t") # this is NOK
"a\t\\t"
julia> replace("a\t", grp => s"\1\t") # this is ERROR
So if we want to use “\1” regex syntax we could not use String, we need Base.SubstitutionString. But there is problem because we need some kind of mix raw string syntax with string syntax …
This hack works but it looks not like “fresh approach”:
julia> replace("a\t", grp => @s_str("\\1\t")) # using hack is OK
"a\t\t"
We very probably need to fix functions working with SubstitutionString. (replace doesn’t understand ‘\t’ in substitution string)
It could be disputable. Problem described in SO seems to be still solvable only by using hack.
I don’t see this as a hack. As you point out on SO, you can always do Base.SubstitutionString("....some literal string....") with as many backslashes as necessary, or @s_str("...") as you note above. For example:
Sometimes you need to call the raw constructor in order to have fine control over what is escaped. This is also important for constructing the string programmatically (not by a literal).
That being said, in cases where it can be done without ambiguity, it would be great to extend the range of escapes recognized by s"..." etcetera.
Currently, I just use Base.SubstitutionString, although that may change (I have had to make my own types to replace Regex, RegexMatch, and RegexMatchIterator, to support string types other than String)