# RegEx in Painless illegal expression

**URL:** <https://discuss.elastic.co/t/regex-in-painless-illegal-expression/270040>\
**Category:** Kibana\
**Created:** [April 13, 2021, 4:34pm UTC](https://discuss.elastic.co/t/regex-in-painless-illegal-expression/270040 "2021-04-13T16:34:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamesm1](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@jamesm1](https://discuss.elastic.co/u/jamesm1)\
**Post date:** [April 13, 2021, 4:34pm UTC](https://discuss.elastic.co/t/regex-in-painless-illegal-expression/270040/1 "2021-04-13T16:34:30Z")

</div>

Hello - I am using the Dev Tool for painless and i am trying to get a RegEx statement to work. The statement is :

```auto
def path = "/v3/api/users/stuff./games";
if (path =~ /^\/v3\/api\/users\/([^\/]+)$/) {
    path = "bob";
    return path
} else if (path =~ /v3\/api\/users\/([^\/]+)(\/[^\/]+)?){
    path = "steve";
    return path
}

```

However this is the error message i am getting back and i am not sure why ☹

```auto
Unhandled Exception illegal_argument_exception

unexpected token [']'] was expecting one of [')'].

Stack:
[
  "... i\\/users\\/([^\\/]+)(\\/[^\\/]+)?){\n path = \"steve\" ...",
  " ^---- HERE"
]

```

Thank you in advnace

---

<div class="post-metadata">

**Author:** ![Felix\_Roessel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felix_roessel/32/41623_2.png) [@Felix\_Roessel](https://discuss.elastic.co/u/Felix_Roessel)\
**Post date:** [April 13, 2021, 4:52pm UTC](https://discuss.elastic.co/t/regex-in-painless-illegal-expression/270040/2 "2021-04-13T16:52:36Z")

</div>

One problem is that your pattern is not matching the . Sign.  
In your second pattern you also have more closing brackets than opening ones

---

<div class="post-metadata">

**Author:** ![jamesm1](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@jamesm1](https://discuss.elastic.co/u/jamesm1)\
**Post date:** [April 14, 2021, 8:02am UTC](https://discuss.elastic.co/t/regex-in-painless-illegal-expression/270040/3 "2021-04-14T08:02:32Z")

</div>

I have popped that RegEx into a debugger and it matches correctly. As for too many closing brackets, Im not sure i see where as I have one set of brackets for the `IF` statement and then 2 sets of brackets for the RegEx groups?  
Please can you tell me where abouts i am going wrong with this :)?

---

<div class="post-metadata">

**Author:** ![Felix\_Roessel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felix_roessel/32/41623_2.png) [@Felix\_Roessel](https://discuss.elastic.co/u/Felix_Roessel)\
**Post date:** [April 14, 2021, 8:17am UTC](https://discuss.elastic.co/t/regex-in-painless-illegal-expression/270040/4 "2021-04-14T08:17:45Z")

</div>

Ahh okay so the pattern is  
`v3\/api\/users\/([^\/]+)(\/[^\/]+)?`

But this needs to have a starting `/` and a closing `/` I think.  
You only have one at the beginning. Maybe thats the issue?

---

<div class="post-metadata">

**Author:** ![jamesm1](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@jamesm1](https://discuss.elastic.co/u/jamesm1)\
**Post date:** [April 14, 2021, 8:22am UTC](https://discuss.elastic.co/t/regex-in-painless-illegal-expression/270040/5 "2021-04-14T08:22:45Z")

</div>

My statement now looks like

```auto
def path = "/v3/api/users/stuff./games";
path = /^\/v3\/api\/users\/([^\/]+)/;
return path;
if (path =~ /^\/v3\/api\/users\/([^\/]+)$/) {
    path = "bob";
} else if (path =~ /v3\/api\/users\/([^\/]+)(\/[^\/]+)?/)
{
    path = "steve";
    return path
}
path;

```

I no longer get issues in the input however i am still getting this message back.

```auto
Unhandled Exception illegal_argument_exception

invalid block: unreachable statement

Stack:
[
  "def path = \"/v3/api/users ...",
  "^---- HERE"
]

```

I understand that `/` is a special character in JavaScript/Painless... could this cause an issue?

---

<div class="post-metadata">

**Author:** ![jamesm1](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@jamesm1](https://discuss.elastic.co/u/jamesm1)\
**Post date:** [April 14, 2021, 8:34am UTC](https://discuss.elastic.co/t/regex-in-painless-illegal-expression/270040/6 "2021-04-14T08:34:41Z")

</div>

If i remove the last `/` in the 2nd line - as this isnt needed as its not part of my pattern, i am returning the original error again.

---

<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:** [May 12, 2021, 8:35am UTC](https://discuss.elastic.co/t/regex-in-painless-illegal-expression/270040/7 "2021-05-12T08:35:11Z")

</div>

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