author | cu |
Thu, 28 Sep 2017 11:04:11 +0100 | |
changeset 510 | 25580bf89ac0 |
parent 420 | 25bc57b32efa |
child 558 | 447ed6c7cdad |
permissions | -rwxr-xr-x |
49 | 1 |
#!/usr/bin/env python |
2 |
import re |
|
3 |
import sys |
|
4 |
||
420
25bc57b32efa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
411
diff
changeset
|
5 |
# case of catastrophic backtracking in Python |
25bc57b32efa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
411
diff
changeset
|
6 |
# |
25bc57b32efa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
411
diff
changeset
|
7 |
# regex: (a?){n} a{n} |
25bc57b32efa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
411
diff
changeset
|
8 |
# strings: aa... |
25bc57b32efa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
411
diff
changeset
|
9 |
# |
25bc57b32efa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
411
diff
changeset
|
10 |
# call with timing as: |
25bc57b32efa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
411
diff
changeset
|
11 |
# |
25bc57b32efa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
411
diff
changeset
|
12 |
# > time ./catastrophic.py 20 |
25bc57b32efa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
411
diff
changeset
|
13 |
|
25bc57b32efa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
411
diff
changeset
|
14 |
# counter n given on the command line |
49 | 15 |
cn = sys.argv[1] |
16 |
||
420
25bc57b32efa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
411
diff
changeset
|
17 |
# constructing the regex |
49 | 18 |
r1 = '((a?){%s})' % cn |
19 |
r2 = 'a{%s}' % cn |
|
20 |
||
420
25bc57b32efa
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
411
diff
changeset
|
21 |
# calling the matching function |
49 | 22 |
m = re.match(r1 + r2 , "a" * int(cn)) |
23 |
||
24 |
print m.group(0) |