# How to create a regex from a string with metacharacters?

**URL:** https://discourse.julialang.org/t/how-to-create-a-regex-from-a-string-with-metacharacters/33898
**Category:** General Usage
**Tags:** question, regex
**Created:** [January 28, 2020, 6:10pm UTC](https://discourse.julialang.org/t/how-to-create-a-regex-from-a-string-with-metacharacters/33898 "2020-01-28T18:10:02Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Dictino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dictino/32/217281_2.png) [@Dictino](https://discourse.julialang.org/u/Dictino)
#### Post date: [January 28, 2020, 6:10pm UTC](https://discourse.julialang.org/t/how-to-create-a-regex-from-a-string-with-metacharacters/33898/1 "2020-01-28T18:10:02Z")

</div>

Hi

I need to look for a set of sub-strings in a long text and there are two obvious options at hand: `regex` and `findfirst`.

`findfirst` is nice and does exactly the thing I want, but it is a bit slow (10 times slower that using a regex on Linux, and only little bit more slower on Windows 10, see a previous post [Regex performance on Windows 10](https://discourse.julialang.org/t/regex-performance-on-windows-10/32113) for more details).

I’ve tried to “increase the speed” of `findfirst` developing my own version with little success and I have no hope to get a performance comparable to regex because it make use of `memchr` wich in Linux is pretty good optimized (see [Can this loop be optimized further?](https://discourse.julialang.org/t/can-this-loop-be-optimized-further/32510))

The _problem_ with the `regex` is that the sub-string could be an **arbitrary** text and contain meta-characters, so I need to escape the regex MWE:

```julia
r=Regex("he.lo")
match(r,"hello world")
#RegexMatch("hello") bad, I don't want to match anything there

r=Regex(replace("he.lo", "."=>"\\."))
match(r,"hello world")
#nothing, that is what I want

```

But this workaround is not general enough… what if the string contains other metacharacters ([,?,$,^… and so on) ?

Is there a robust way to escape the Regex string?

Thanks in advance!

---

<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: [January 28, 2020, 6:59pm UTC](https://discourse.julialang.org/t/how-to-create-a-regex-from-a-string-with-metacharacters/33898/2 "2020-01-28T18:59:00Z")

</div>

How about a regex? 🙂

```julia
julia> Regex(replace("h[el.o", r"([\\\^\$\.\|\?\*\+\(\)\[\]])" => s"\\\1"))
r"h\[el\.o"

```

---

<div class="post-metadata">

### Author: ![Dictino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dictino/32/217281_2.png) [@Dictino](https://discourse.julialang.org/u/Dictino)
#### Post date: [January 28, 2020, 7:22pm UTC](https://discourse.julialang.org/t/how-to-create-a-regex-from-a-string-with-metacharacters/33898/3 "2020-01-28T19:22:13Z")

</div>

This is… a meta-regex 😉 it blows my mind 🤯  
Thank you @pfitzseb!
