diff --git a/exrex.py b/exrex.py index d86622c..21d2c2b 100644 --- a/exrex.py +++ b/exrex.py @@ -183,7 +183,7 @@ def _gen(d, limit=20, count=False, grouprefs=None): if count: strings = (strings or 1) * len(subs) ret = comb(ret, subs) - elif i[0] == sre_parse.MAX_REPEAT: + elif i[0] == sre_parse.MAX_REPEAT or i[0] == sre_parse.MIN_REPEAT: items = list(i[1][2]) if i[1][1] + 1 - i[1][0] >= limit: ran = range(i[1][0], i[1][0] + limit) @@ -256,7 +256,7 @@ def _randone(d, limit=20, grouprefs=None): ret += choice(CATEGORIES.get(i[1], [''])) elif i[0] == sre_parse.ANY: ret += choice(CATEGORIES['category_any']) - elif i[0] == sre_parse.MAX_REPEAT: + elif i[0] == sre_parse.MAX_REPEAT or i[0] == sre_parse.MIN_REPEAT: if i[1][1] + 1 - i[1][0] >= limit: min, max = i[1][0], i[1][0] + limit - 1 else: @@ -343,6 +343,16 @@ def sre_to_string(sre_obj, paren=True): else: range_str = '{{{0},{1}}}'.format(i[1][0], i[1][1]) ret += sre_to_string(i[1][2], paren=paren) + range_str + elif i[0] == sre_parse.MIN_REPEAT: + if i[1][0] == 0 and i[1][1] == sre_parse.MAXREPEAT: + range_str = '*?' + elif i[1][0] == 1 and i[1][1] == sre_parse.MAXREPEAT: + range_str = '+?' + elif i[1][1] == sre_parse.MAXREPEAT: + range_str = '{{{0},}}?'.format(i[1][0]) + else: + range_str = '{{{0},{1}}}?'.format(i[1][0], i[1][1]) + ret += sre_to_string(i[1][2], paren=paren) + range_str elif i[0] == sre_parse.GROUPREF: ret += '\\{0}'.format(i[1]) elif i[0] == sre_parse.AT: diff --git a/tests.py b/tests.py index 70740e6..2784cfc 100644 --- a/tests.py +++ b/tests.py @@ -52,6 +52,8 @@ BIGS = [ '^[^/]+(.*)$', '^([^/]+).*$', '^[^asdf]+$', + '^a{5,}?', + '^a[^bcd]*?e', '^([^0-9]{2,}|(a|s|d|f|g)+|[a-z]+)+$', '^([^ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]|asdf)+$', '^(1[0-2]|0[1-9])(:[0-5]\d){2} (A|P)M$',