I decided, I think. Thanks to unutbu for pointing out
sre_parse !
import sre_parse def get_regex_max_match_len(regex): minlen, maxlen = sre_parse.parse(regex).getwidth() if maxlen >= sre_parse.MAXREPEAT: raise ValueError('unbounded regex') return maxlen
Results in:
>>> get_regex_max_match_len('foo((bar){2,3}|potato)') 12 >>> get_regex_max_match_len('.*') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in get_regex_max_match_len ValueError: unbounded regex
source share