Hi!
I’m not an expert in regular expressions and this seems like a trivial problem to me. Unfortunatley I couldn’t find anything by googling.
Problem: I want to replace a \s in a regular expression with \t. This is what I tried:
replace("x 3", r"x (?<x>\d*)", s"y\t\g<x>")
But I get a Bad replacement string error. Also other special characters like \s and '\ndon't work. Am I missing something or doing something wrong? Unfortunatley things likes"\t"yield a literal"\t"`.
The issue is really that the s"…" and r"…" strings have their own syntax, which is not compatible with the normal string escaping.
You’d need to either put the tab in directly (which makes it hard to read), or use @dan’s workaround (which is what I’ve done in the past)
A better version of the workaround is @s_str("y\t\\f<x>"). Calling the macro directly without the tagged string literal, causes the string to be parsed with the regular escaping. Much shorter code.
Maybe I am wrong but still have feeling this is a bug:
julia> macro tst_str(string) println([i for i in string]) end;
# so far so good:
julia> @tst_str("a")
['a']
julia> tst"a"
['a']
# this is probably wanted to help create raw_str:
julia> @tst_str("\\a")
['\\', 'a']
julia> tst"\\a"
['\\', '\\', 'a']
# but there is a problem to add backslash at the end of string:
julia> @tst_str("a\\")
['a', '\\']
julia> tst"a\\"
['a', '\\', '\\']
julia> tst"a\" # 3times <enter> in REPL
ERROR: syntax: incomplete: invalid string syntax
Maybe enhance syntax for string macros could help? I mean something like:
macro a_str(a) # for raw "escaping"
macro a_estr(a) # for standard escaping
# but macros above would have problem with name clash...
a"how to interpret this o\ne?"