Back to Elasticsearch

Regexes [painless-regexes]

docs/reference/scripting-languages/painless/painless-regexes.md

9.4.01.6 KB
Original Source

Regexes [painless-regexes]

Regular expression constants are directly supported. To ensure fast performance, this is the only mechanism for creating patterns. Regular expressions are always constants and are compiled a single time for efficiency.

You can check the regular expressions tutorial for related examples, and for help with any required debugging, refer to Debug regex pattern matching failures in Painless.

painless
Pattern p = /[aeiou]/

::::{warning} A poorly written regular expression can significantly slow performance. If possible, avoid using regular expressions, particularly in frequently run scripts. ::::

Pattern flags [pattern-flags]

You can define flags on patterns in Painless by adding characters after the trailing / like /foo/i or /foo \w #comment/iUx. Painless exposes all of the flags from Java’s Pattern class using these characters:

CharacterJava ConstantExample
cCANON_EQ'å' ==~ /å/c (open in hex editor to see)
iCASE_INSENSITIVE'A' ==~ /a/i
lLITERAL'[a]' ==~ /[a]/l
mMULTILINE'a\nb\nc' =~ /^b$/m
sDOTALL (aka single line)'a\nb\nc' =~ /.b./s
UUNICODE_CHARACTER_CLASS'Ɛ' ==~ /\\w/U
uUNICODE_CASE'Ɛ' ==~ /ɛ/iu
xCOMMENTS (aka extended)'a' ==~ /a #comment/x