Search code examples
regexamazon-web-servicesregex-lookaroundsregexp-replace

Regex for match sequence digits in Amazon Redshift


I am write a regex that matches sequence increasing numbers in string. It works fine in test, but does not works in Amazon Redshift. Can anyone help me out here

Regex:

^(?:[^1]*)1(?:[^2]*)2(?:[^3]*)3(?:.*)$

For example it match strings (correct):

123
1331213
12222211323
17397462453
1abcd2abcd3abcd

https://regex101.com/r/zkUMxG/1/

When I use it in Amazon, get error:

Error running query: Invalid preceding regular expression prior to repetition operator. The error occurred while parsing the regular expression fragment: '(?>>>HERE>>>:[^1]*)1(?'. DETAIL: ----------------------------------------------- error: Invalid preceding regular expression prior to repetition operator. The error occurred while parsing the regular expression fragment: '(?>>>HERE>>>:[^1]*)1(?'. code: 8002 context: T_regexp_init query: 19124520 location: funcs_expr.cpp:185 process: query1_107_19124520 [pid=15061] -----------------------------------------------

Solution

  • Amazon Redshift regex is POSIX-based (proof). POSIX Basic Regular Expressions don't support non-capturing groups that your ^(?:[^1]*)1(?:[^2]*)2(?:[^3]*)3(?:.*)$ expression contains: there are four (?:...) patterns.

    Remove the groups altogher as you do not plan on using them:

    ^[^1]*1[^2]*2[^3]*3.*$