progs/catastrophic/catastrophic2.py
author Christian Urban <christian.urban@kcl.ac.uk>
Wed, 17 Sep 2025 14:29:52 +0100
changeset 985 c7e944977e39
parent 753 d94fdbef1a4f
permissions -rwxr-xr-x
updated

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

# case of catastrophic backtracking in Python
#
# regex: (a*)*b
# strings: aa...a
#
# call with timing as:
#
#    time ./catastrophic2.py 20

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

# calling the matching function
s = ("a" * int(cn))
m = re.fullmatch('(a*)*b' , s) 

print(s)
print(bool(m))