Search code examples
phppythonregexperl

Pathological regex that blows up (time & memory)?


What's a pathological regex that blows up many parsers (both in time & memory)? and which parsers? Bonus points the more basic and standard the regex is, and the more likely that a non-malicious user might innocently come up with it. Feel free to post actual time and memory data, and parser version.

(I seem to remember that excessive lookbehind assertions or (EDIT:)backtracking in Perl are said to do this, or at least used to be. Anything else?)


Solution

  • Adapted from the first example in the article Regular Expression Matching Can Be Simple And Fast (but is slow in Java, Perl, PHP, Python, Ruby, ...):

    perl -e '$n=29; ("a" x $n) =~ (("a?" x $n).("a" x $n))'
    

    Which takes 40+ seconds on my system. Then do $n++ for exponentially increasing fun...