# Escaping special characters in regex

**URL:** <https://discuss.elastic.co/t/escaping-special-characters-in-regex/173071>\
**Category:** Elasticsearch\
**Created:** [March 20, 2019, 4:18am UTC](https://discuss.elastic.co/t/escaping-special-characters-in-regex/173071 "2019-03-20T04:18:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pk.241011](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pk.241011/32/86285_2.png) [@pk.241011](https://discuss.elastic.co/u/pk.241011)\
**Post date:** [March 20, 2019, 4:18am UTC](https://discuss.elastic.co/t/escaping-special-characters-in-regex/173071/1 "2019-03-20T04:18:59Z")

</div>

I was writing pipeline and in the gsub processor I wrote a regex. As per the [documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html)

```
Allowed characters

Any Unicode characters may be used in the pattern, but certain characters are reserved and must be escaped. The standard reserved characters are:

. ? + * | { } [] ( ) " \

```

I had to escape the double quotes but not the other characters. Why?  
`"pattern": "\\\"\\\"(?=[a-zA-Z0-9])"` worked.

I was using the kibana dev tool. Is it because of that that I have to use 3 slashes to escape the double quote? I was expecting to use 2 slashes.  
I am not an expert on regex but will like to have some clarity on this.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 20, 2019, 1:26pm UTC](https://discuss.elastic.co/t/escaping-special-characters-in-regex/173071/2 "2019-03-20T13:26:02Z")

</div>

hey,

I agree that is somewhat confusing. So the basics are covered by the java rules of regexes, however being inside of JSON requires another round of escaping, so that when the JSON is parsed you keep the rules of the regular expression (sort of unpacking the escaping at different stages of parsing, once when parsing JSON, once when parsing the regex). Hope that makes sense.

--Alex

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 17, 2019, 1:26pm UTC](https://discuss.elastic.co/t/escaping-special-characters-in-regex/173071/3 "2019-04-17T13:26:09Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
