RegEx in Painless illegal expression

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

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 :frowning:

Unhandled Exception illegal_argument_exception

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

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

Thank you in advnace

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

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 :)?

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?

My statement now looks like

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.

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?

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.

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