Regular Expressions - Can't Insert \t in Replacement String

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"`.

Have you tried escaping the \?

Oh sorry something went wrong with the format there…
Typing s"y\t\g<x>" yields an error s"y\\t\g<x>" yields s"y\\\\t\\g<x>".

Writing explicitly Base.SubstitutionString("y\t\\g<x>") might be a workaround.


That does indeed work. Thank you!

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.

The whole line is now:

println(replace("x 3", r"x (?<x>\d*)", @s_str("d\t\\g<x>")))

That’s a nice general technique!

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")
julia> tst"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?"
1 Like

:tada: At least Julia 0.7.0-DEV.2815:

julia> tst"a\\"
['a', '\\']