# Strange regex error (bug?)

**URL:** https://discourse.julialang.org/t/strange-regex-error-bug/84759
**Category:** General Usage
**Tags:** regex
**Created:** [July 25, 2022, 3:02pm UTC](https://discourse.julialang.org/t/strange-regex-error-bug/84759 "2022-07-25T15:02:45Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)
#### Post date: [July 25, 2022, 3:02pm UTC](https://discourse.julialang.org/t/strange-regex-error-bug/84759/1 "2022-07-25T15:02:45Z")

</div>

```julia
julia> r"^\s*text"
r"^\s*text"

julia> Regex("^\s*text")
ERROR: syntax: invalid escape sequence

```

Julia 1.7.3 on Win10

And BTW what does the following error message mean?

```julia
julia> r"^[:space:]*text"
ERROR: LoadError: PCRE compilation error: POSIX named classes are supported only within a class at offset 1

```

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [July 25, 2022, 3:06pm UTC](https://discourse.julialang.org/t/strange-regex-error-bug/84759/2 "2022-07-25T15:06:06Z")

</div>

> [@Eben60](#):
>
> `Regex("^\s*text")`

```julia
julia> Regex("^\\s*text")
r"^\s*text"

```

---

<div class="post-metadata">

### Author: ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)
#### Post date: [July 25, 2022, 5:44pm UTC](https://discourse.julialang.org/t/strange-regex-error-bug/84759/3 "2022-07-25T17:44:26Z")

</div>

What @jling is saying is you need escaping, and note you could have done it this way `r"^\\s*text"`

but note also, instead of needing escaping (for regular strings) you can use the raw type, like `raw"^\s*text"`

That gives you a string, not a Regex, so you would still need Regex(raw"^\s\*text") to make it so. That can help if you would otherwise need lots of escaping.

This got me thinking (since the r and raw strings are implemented by macros, r\_str and raw\_str), can you do macro composition (or here for string-types, so indirectly)?

`r∘raw"^\s*text"` doesn’t work (and maybe it shouldn’t…), nor does `(r∘raw)"^\s*text"`. I’m ok with not having the option, since it’s likely very rare to do…

but it got me thinking, should this have been the default for Julia’s regexes, or at least also be provided?

A r\_raw type of regex/string (or rather name it raw\_r?) could be made. I’m a bit stumped how. The former is implemented like:

```julia
macro r_str(pattern, flags...) Regex(pattern, flags...) end

```

and raw by:

```julia
macro raw_str(s); s; end

```

> [@Eben60](#):
>
> BTW what does the following error message mean?
> 
> ```julia
> julia> r"^[:space:]*text"
> ERROR: LoadError: PCRE compilation error: POSIX named classes are supported only
> 
> ```

Note, PCRE is the regex engine (from Perl) library used in Julia, so the error comes from it, since something wrong with your regex, seemingly this (since Julia is 1-based, should mean the same as “offset 0” there)::

> <https://stackoverflow.com/questions/475159/php-regex-what-is-class-at-offset-0>

I didn’t read carefully, but it seems your fix may be as simple as `r"^[[:space:]]*text"` in a sense needing a type of intentional “escaping”, if you will).

---

<div class="post-metadata">

### Author: ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)
#### Post date: [July 25, 2022, 6:00pm UTC](https://discourse.julialang.org/t/strange-regex-error-bug/84759/4 "2022-07-25T18:00:03Z")

</div>

> [@Palli](#):
>
> I didn’t read carefully, but it seems your fix may be as simple as `r"^[[:space:]]*text"` in a sense needing a type of intentional “escaping”, if you will).

Yes, that works 🙂

```julia

julia> rg = r"^[[:space:]]*text"
r"^[[:space:]]*text"

julia> occursin(rg, " text about")
true

```
