Regex hangs in Oracle 11g Regex hangs in Oracle 11g oracle oracle

Regex hangs in Oracle 11g


Your regex is causing catastrophic backtracking.

Briefly, your regex has terms that can both capture the same part of the input, but fails to do so. The regex engine must try all combinations before failing and due to the matching tree created, each extra character doubles the number of ways the match can be made. Creating and traversing this tree leads to a geometrically exponential execution time proportional to 2^n - which you are seeing.

You may find that changing your dual expression to a possessive quantifier (ie ++ rather than +) stops this behaviour, because with ++ once characters are consumed, they stay consumed.


Incidentally, this expression

[\-a-zA-z0-9_''^&\+\?\:]

may be rewritten as:

[-\w''^&+?:]

Because:

  • inside a character class (almost) all characters lose their special regex meaning
  • a dash first or last is a literakl dash (not a range)
  • \w == [a-zA-Z0-9_]