Though I just realized "regex parser" can mean either "parsing some text using regex" or "code that compiles regex syntax into some kind of data structure but does not actually do anything else". The latter obviously is a much easier task.
The minimum length will be the number of dots without a quantifier (i.e. exactly one character). If there is any dot followed by an asterisk, there is no maximum length. Otherwise, the maximum length will be the minimum length plus the number of dots followed by a question mark.
Writing the code for this is left as an exercise to the reader. ;)
def matches(pattern, s):
if len(pattern) == 0:
return True
c = pattern[0]
assert c not in ("*", "?")
if len(pattern) > 1:
if pattern[1] == "?":
return (
len(s) > 0 and c in (".", s[0]) and matches(pattern[2:], s[1:])
) or matches(pattern[2:], s)
elif pattern[1] == "*":
for i, d in enumerate(s):
if c not in (".", d):
break
return any(matches(pattern[2:], s[j:]) for j in range(i, -1, -1))
return len(s) > 0 and c in (".", s[0])