# HG changeset patch # User Christian Urban # Date 1543457885 0 # Node ID bfd511b7ecbf7360cc2cbca6f6cc7d5e6599ffe9 # Parent 7a12053567d4497c04ebba4a5d5af0d9b19331d5 updated diff -r 7a12053567d4 -r bfd511b7ecbf progs/catastrophic.py --- a/progs/catastrophic.py Wed Nov 28 23:45:37 2018 +0000 +++ b/progs/catastrophic.py Thu Nov 29 02:18:05 2018 +0000 @@ -2,34 +2,20 @@ import re import sys -# A case of catastrophic backtracking in Python -# -# regex: (a?){n} a{n} -# strings: aa... -# -# call with: +# case of catastrophic backtracking in Python # -# > ./catastrophic.py 20 -# -# or -# -# > ./catastrophic.py 28 -# +# regex: (a*)*b +# strings: aa...a # # call with timing as: # # > time ./catastrophic.py 20 - - # counter n given on the command line cn = sys.argv[1] -# constructing the regex -r1 = '((a?){%s})' % cn -r2 = 'a{%s}' % cn +# calling the matching function +s = ("a" * int(cn)) +m = re.match('(a*)*b' , s) -# calling the matching function -m = re.match(r1 + r2 , "a" * int(cn)) - -print m.group(0) +print s