Regex escape chars

I’m currently trying to use ArgParse so that a user can change a regex pattern in the code.
I encountered the following issue with Regex during that.
Why is Regex("\.git") not valid?
The error is invalid escape sequence but r"\.git" works as expected.
I have to use Regex("\\.git") which is a little confusing.
Can anyone explain why this is the case?

Regex is a function and, other than a macro, it’s argument get’s evaluated before the function is called.
The error stems from the string "\.git" alone, see

julia> "\."
ERROR: syntax: invalid escape sequence

This is because characters after backslashes in strings are always escaped and \. is no valid escape character. (For motivation see e.g. string literals: disallow backslash before non-escapes · Issue #21284 · JuliaLang/julia · GitHub and python - How to replace string literals back/front slashes in Julia? - Stack Overflow)

String literals (such as r"\.git") obey different rules which I don’t completely understand but at least they don’t require always every backlash escaping the following symbol, see e.g. discussions here: https://github.com/JuliaLang/julia/issues/22926

4 Likes

Mostly the content of a non-standard string literal is taken verbatim and passed to the macro, which in this case passes that string directly to PCRE, which interprets the meaning according to this:

https://www.pcre.org/original/doc/html/pcrepattern.html

The only case where the contents of a non-standard string is not passed literally to the defining macro is if there are backslashes followed by a double quote character: if the number of backslashes is even, you get n/2 backslashes in the string followed by end-of-string; if the number of backslashes is odd, you get (n-1)/2 backslashes followed by a quote character.

2 Likes

Another example of a string literal is the raw string literal (but not particularly useful in this case).

julia> raw"\.git"
"\\.git"

julia> Regex(raw"\.git")
r"\.git"

The raw string literal is really useful for Windows file paths:

julia> raw"C:\Users\Greg\Data\Julia"
"C:\\Users\\Greg\\Data\\Julia"
3 Likes