# Match a string literal via regex

**URL:** <https://discourse.julialang.org/t/match-a-string-literal-via-regex/24002>\
**Category:** General Usage\
**Tags:** question, strings, regex\
**Created:** [May 8, 2019, 4:56pm UTC](https://discourse.julialang.org/t/match-a-string-literal-via-regex/24002 "2019-05-08T16:56:42Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [May 8, 2019, 4:56pm UTC](https://discourse.julialang.org/t/match-a-string-literal-via-regex/24002/1 "2019-05-08T16:56:42Z")

</div>

Just like `'"([^\\"]+|\\.)*?"'` in Python or `"\G\"([^\\\"]+|\\\.)*?\""` in C#/F#, it could build a regular expression to parse a whole text like

```julia
"xxxxxxxxxx\"xxxxxxxxxxxxxx\""

```

However things seem not work in Julia.

```julia
regex_s = raw"\"([^\\\"]+|\\\.)*?\""
regex = Regex(raw"\G" * regex_s)
str = repr("xxxxxxxxxx\"xxxxxxxxxxxxxx\"")
match(r, z)
# RegexMatch("\"xxxxxxxxxx\\\"", 1="xxxxxxxxxx\\")

```

Any workaround?

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [May 8, 2019, 5:34pm UTC](https://discourse.julialang.org/t/match-a-string-literal-via-regex/24002/2 "2019-05-08T17:34:17Z")

</div>

Not sure if this is what you want?

```julia
julia> str = repr("xxxxxxxxxx\"xxxxxxxxxxxxxx\"")
"\"xxxxxxxxxx\\\"xxxxxxxxxxxxxx\\\"\""

julia> collect(m.match for m in eachmatch(r"[^\"]+", str))
2-element Array{SubString{String},1}:
 "xxxxxxxxxx\\"    
 "xxxxxxxxxxxxxx\\"

```

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [May 8, 2019, 5:40pm UTC](https://discourse.julialang.org/t/match-a-string-literal-via-regex/24002/3 "2019-05-08T17:40:08Z")

</div>

I want to match a String/SubString to check if it starts with with a string(`"...\"..."`), and if so I extract it out of the head of String/SubString(then I’ll get a new SubString).

```julia
m = match(str_regex, str)
if m === nothing
   @fail # fail and jump out of here
else
   token = m.match
   push!(tokens, token)
   str = SubString(str, length(token))
end

```

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [May 8, 2019, 5:41pm UTC](https://discourse.julialang.org/t/match-a-string-literal-via-regex/24002/4 "2019-05-08T17:41:44Z")

</div>

Also, a concrete use case is a [parser generator with automatic lexers](https://github.com/thautwarm/RBNF.jl/blob/master/src/Lexer.jl#L9)

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [May 9, 2019, 2:32pm UTC](https://discourse.julialang.org/t/match-a-string-literal-via-regex/24002/5 "2019-05-09T14:32:32Z")

</div>

I don’t really understand your question in the OP (I don’t know well python/c# regexes, and you have some typos), but if I understand the title correctly, you can quote a string by enclosing it with `\Q` and `\E`, e.g. `Regex(raw"\Q" * str * raw"\E")`.

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [May 9, 2019, 2:50pm UTC](https://discourse.julialang.org/t/match-a-string-literal-via-regex/24002/6 "2019-05-09T14:50:52Z")

</div>

Sorry that I’m not to match a specific string.  
Wen writing string literals in our codes, we write a `"` firstly, then followed by a sequence of characters, and finally we’d write another `"` to end this process.

```julia
str = "<a sequence>"

```

Furthermore, when we want to represent a string contains `"`s, we have to escape them in this way: `"xx\"xx"`.

Note that our source codes are not special, I mean they’re still plain text.  
So, how do programming language compilers parse the literal strings?  
One way is using regular expression, which is quite mature and capable of expressing/matching escapes and quotations.  
My problem is Julia didn’t work in this scope.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [May 9, 2019, 3:07pm UTC](https://discourse.julialang.org/t/match-a-string-literal-via-regex/24002/7 "2019-05-09T15:07:06Z")

</div>

are you looking for this?

```julia
julia> str_regex = r"(...\"...)(.*)"
r"(...\"...)(.*)"

julia> test = "...\"... abc"
"...\"... abc"

julia> m = match(str_regex, test)
RegexMatch("...\"... abc", 1="...\"...", 2=" abc")

julia> m.captures[2]
" abc"

```

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [May 9, 2019, 6:00pm UTC](https://discourse.julialang.org/t/match-a-string-literal-via-regex/24002/8 "2019-05-09T18:00:26Z")

</div>

thank you, but not this.  
Given a text file, whose content looks like

```julia
"this is a str\"ing"

```

Then read it into Julia, so how can we match it?

---

<div class="post-metadata">

**Author:** ![tkluck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkluck/32/15769_2.png) [@tkluck](https://discourse.julialang.org/u/tkluck)\
**Post date:** [May 9, 2019, 6:19pm UTC](https://discourse.julialang.org/t/match-a-string-literal-via-regex/24002/9 "2019-05-09T18:19:58Z")

</div>

If I understand you correctly, you want to match a “string literal”, not match a “literal string”. (You could edit your post’s title.)

Languages are subtly different on what escaping rules apply; see e.g. [https://github.com/JuliaLang/julia/issues/22926](https://github.com/JuliaLang/julia/issues/22926) for a nice discussion about a (fixed) corner case in Julia. Do you have any specific language you want to emulate?

---

<div class="post-metadata">

**Author:** ![tkluck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkluck/32/15769_2.png) [@tkluck](https://discourse.julialang.org/u/tkluck)\
**Post date:** [May 9, 2019, 6:22pm UTC](https://discourse.julialang.org/t/match-a-string-literal-via-regex/24002/10 "2019-05-09T18:22:46Z")

</div>

This one might be helpful to what you’re trying to achieve: [String literals and regular expressions](http://wordaligned.org/articles/string-literals-and-regular-expressions) . After all, your question is about regular expressions much more than it is about Julia.

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [May 9, 2019, 6:45pm UTC](https://discourse.julialang.org/t/match-a-string-literal-via-regex/24002/11 "2019-05-09T18:45:40Z")

</div>

> your question is about regular expressions much more than it is about Julia.

Somewhat don’t agree. I know how to write this regex but don’t know why I fail this when with Julia.

---

<div class="post-metadata">

**Author:** ![tkluck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkluck/32/15769_2.png) [@tkluck](https://discourse.julialang.org/u/tkluck)\
**Post date:** [May 9, 2019, 7:22pm UTC](https://discourse.julialang.org/t/match-a-string-literal-via-regex/24002/12 "2019-05-09T19:22:39Z")

</div>

> [@thautwarm](#):
>
> Somewhat don’t agree. I know how to write this regex but don’t know why I fail this when with Julia.

You are right. I think what’s tripping you up is more likely than not what I linked above: [behaviour for quotes following slashes inside raw strings](https://docs.julialang.org/en/v1/manual/strings/index.html#man-raw-string-literals-1). Specifically, you have `\\"` inside your character class, and that’s interpreted differently in Julia than elsewhere.

Here’s the easiest way I found to get it to work. I tried to make it extra readable by using the `x` modifier, which allows whitespace and comments as follows:

```julia
julia> str = repr("xxxxxxxxxx\"xxxxxxxxxxxxxx\"");

julia> regex = r"""
       \G # match start
       \" # opening quote
       (?: # don't capture (better performance)
           [^\"\\]+ # not a quote or a slash
           | # or
           \\. # an escaped character
       )*? # ungreedy multiples of the above
       \" # closing quote
       """x;

julia> match(regex, str)
RegexMatch("\"xxxxxxxxxx\\\"xxxxxxxxxxxxxx\\\"\"", 1="\\\"")

```

Hope that helps!

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [May 10, 2019, 3:22am UTC](https://discourse.julialang.org/t/match-a-string-literal-via-regex/24002/13 "2019-05-10T03:22:28Z")

</div>

Awesome! Also thanks for solving this in such an elegant way!
