# Regex escape chars

**URL:** <https://discourse.julialang.org/t/regex-escape-chars/22363>\
**Category:** General Usage\
**Tags:** strings, regex\
**Created:** [March 26, 2019, 5:50pm UTC](https://discourse.julialang.org/t/regex-escape-chars/22363 "2019-03-26T17:50:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wikunia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wikunia/32/2180_2.png) [@Wikunia](https://discourse.julialang.org/u/Wikunia)\
**Post date:** [March 26, 2019, 5:50pm UTC](https://discourse.julialang.org/t/regex-escape-chars/22363/1 "2019-03-26T17:50:23Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![under-Peter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/under-peter/32/3626_2.png) [@under-Peter](https://discourse.julialang.org/u/under-Peter)\
**Post date:** [March 26, 2019, 6:10pm UTC](https://discourse.julialang.org/t/regex-escape-chars/22363/2 "2019-03-26T18:10:30Z")

</div>

`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
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](https://github.com/JuliaLang/julia/issues/21284) and [python - How to replace string literals back/front slashes in Julia? - Stack Overflow](https://stackoverflow.com/questions/43114125/how-to-replace-string-literals-back-front-slashes-in-julia))

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](https://github.com/JuliaLang/julia/issues/22926)

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [March 27, 2019, 3:16am UTC](https://discourse.julialang.org/t/regex-escape-chars/22363/3 "2019-03-27T03:16:55Z")

</div>

> [@under-Peter](#):
>
> String literals (such as `r"\.git"` ) obey different rules which I don’t completely understand

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](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.

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [March 27, 2019, 4:47am UTC](https://discourse.julialang.org/t/regex-escape-chars/22363/4 "2019-03-27T04:47:26Z")

</div>

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

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

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

```

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

```julia
julia> raw"C:\Users\Greg\Data\Julia"
"C:\\Users\\Greg\\Data\\Julia"

```
