progs/catastrophic3.py
author Christian Urban <urbanc@in.tum.de>
Wed, 08 Feb 2017 11:01:50 +0000
changeset 474 4bdf0dedd708
parent 420 progs/catastrophic.py@25bc57b32efa
permissions -rwxr-xr-x
updated

#!/usr/bin/env python
import re
import sys

# case of catastrophic backtracking in Python
#
# regex: (a?){n} a{n}
# strings: aa...
#
# call with timing as:
#
#   > time ./catastrophic.py 20

# counter n given on the command line
cn = sys.argv[1]

# constructing the regex
r = '(.*)a(.{%s})bc' % cn

# calling the matching function
#m = re.match(r, "axaybzbc") 
m = re.match(r, "a" * 100 + "a" * int(cn) + "bc") 

print m.group(0)