# How to use Regex() function?

**URL:** https://discourse.julialang.org/t/how-to-use-regex-function/48891
**Category:** General Usage
**Tags:** regex
**Created:** [October 23, 2020, 4:00pm UTC](https://discourse.julialang.org/t/how-to-use-regex-function/48891 "2020-10-23T16:00:39Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![JianghuiDu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jianghuidu/32/8028_2.png) [@JianghuiDu](https://discourse.julialang.org/u/JianghuiDu)
#### Post date: [October 23, 2020, 4:00pm UTC](https://discourse.julialang.org/t/how-to-use-regex-function/48891/1 "2020-10-23T16:00:39Z")

</div>

`r"(?<=NH3\)\[)\w+(?=\])"` is ok but `Regex("(?<=NH3\)\[)\w+(?=\])")` reports

```julia
ERROR: LoadError: syntax: invalid escape sequence

```

why?

---

<div class="post-metadata">

### Author: ![JianghuiDu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jianghuidu/32/8028_2.png) [@JianghuiDu](https://discourse.julialang.org/u/JianghuiDu)
#### Post date: [October 23, 2020, 4:04pm UTC](https://discourse.julialang.org/t/how-to-use-regex-function/48891/3 "2020-10-23T16:04:00Z")

</div>

OK. just figured out… Need to use `\\` instead of `\` . The documentation should have mentioned that …

---

<div class="post-metadata">

### Author: ![christopher-dG](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher-dg/32/7387_2.png) [@christopher-dG](https://discourse.julialang.org/u/christopher-dG)
#### Post date: [October 23, 2020, 4:04pm UTC](https://discourse.julialang.org/t/how-to-use-regex-function/48891/4 "2020-10-23T16:04:21Z")

</div>

The string macro form doesn’t recognize `\` as an escape character, it’s just another character in the regex (which has its own escape functionality according to regex). But in normal Julia strings, if you use `\` it needs to be part of an escape sequence such as `\n` (newline) or `\\` (literal backslash). If you want to use the `Regex` constructor, you can use `Regex(raw"my_string \ with weird escapes")`.

---

<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: [October 23, 2020, 4:07pm UTC](https://discourse.julialang.org/t/how-to-use-regex-function/48891/6 "2020-10-23T16:07:31Z")

</div>

[https://docs.julialang.org/en/v1/manual/metaprogramming/#man-macros](https://docs.julialang.org/en/v1/manual/metaprogramming/#man-macros)

it does by showing you you need `\\` if you’re constructing via `Regex()`

---

<div class="post-metadata">

### Author: ![JianghuiDu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jianghuidu/32/8028_2.png) [@JianghuiDu](https://discourse.julialang.org/u/JianghuiDu)
#### Post date: [October 23, 2020, 6:25pm UTC](https://discourse.julialang.org/t/how-to-use-regex-function/48891/7 "2020-10-23T18:25:20Z")

</div>

Cool. Maybe it should also be in `?help` …

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 24, 2020, 11:18am UTC](https://discourse.julialang.org/t/how-to-use-regex-function/48891/8 "2020-10-24T11:18:16Z")

</div>

> [@JianghuiDu](#):
>
> should also be in `?help` …

This is not what `?help` does (try it out).
