Vittore Gravano
Vittore Gravano

Reputation: 766

Unrecognized character escape '-' | ElasticSearch

I am trying to create regular expression with condition: "Or maslo until space, dash or end of line, or starting with mas and then any character from one to infinity except of space or dash".

{
  "regexp": {
    "name": {
      "value": "(maslo( |-|$)|mas[^ \-]+)"
    }
  }
}

This expression [^ \-] should be correct, according to Regexp Query Documentation, paragraph "Character classes", last line, which says:

[^abc\-] # any character except '-' or 'a' or 'b' or 'c'

But I am receiving error:

{
    "type": "json_parse_exception",
    "reason": "Unrecognized character escape '-' (code 45)\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@2a7b147b; line: 8, column: 47]"
}

Am I missing something? Thanks for any advice.

Upvotes: 3

Views: 2685

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627469

You may put the hyphen at the start of a character class to avoid having to escape it in ES regex:

"(maslo([- ]|$)|mas[^- ]+)"
         ^           ^

Else, you will need to use double backslashes since a single one is used to define regular string escapes and a literal backslash - a regex escape - is formed using two consecutive backslashes.

In the ES regex documentation, only one backslash is used in the examples because those patterns are written in their literal forms, not as string literals.

Upvotes: 4

Related Questions