# Painless regex not match when string have a "\\n"

**URL:** <https://discuss.elastic.co/t/painless-regex-not-match-when-string-have-a-n/253258>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [October 26, 2020, 4:09am UTC](https://discuss.elastic.co/t/painless-regex-not-match-when-string-have-a-n/253258 "2020-10-26T04:09:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![le\_ba\_nam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/le_ba_nam/32/75148_2.png) [@le\_ba\_nam](https://discuss.elastic.co/u/le_ba_nam)\
**Post date:** [October 26, 2020, 4:09am UTC](https://discuss.elastic.co/t/painless-regex-not-match-when-string-have-a-n/253258/1 "2020-10-26T04:09:59Z")

</div>

I have my regex like this:

```auto
def m = /(.+)webservice.*/.matcher(x);if ( m.matches()) {return m.group(1);

```

The problem is it just match when my string have no "\n" like :

```auto
"mbccs_getStatusIncorrectProfile webservice does not exist"

```

and fail:

```auto
"mbccs_getStatusIncorrectProfile webservice does not exist\n"

```

How can I catch in second case? And even with other characters like "\r" ...Thanks.

---

<div class="post-metadata">

**Author:** ![sebastien](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sebastien/32/36119_2.png) [@sebastien](https://discuss.elastic.co/u/sebastien)\
**Post date:** [October 30, 2020, 10:04am UTC](https://discuss.elastic.co/t/painless-regex-not-match-when-string-have-a-n/253258/2 "2020-10-30T10:04:41Z")

</div>

Hi @le_ba_nam

The `\` character is a special char in Regex so you need to escape it.  
Can you try the following

```auto
String escape(def x) { /([\\])/.matcher(x).replaceAll('\\') };
def m = /(.+)webservice.*/.matcher(escape(x));
if ( m.matches()) {return m.group(1);

```

And let me know if it worked?  
Cheers!

---

<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:** [November 27, 2020, 10:05am UTC](https://discuss.elastic.co/t/painless-regex-not-match-when-string-have-a-n/253258/3 "2020-11-27T10:05:01Z")

</div>

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