Reputation: 207
I'm trying to create a regex to validate numbers that checks the year for leap year. here's a part of the code. for some reason this code would let number 4 8 24 28 as a valid regex.
(0{2}?)
([2468][480] | [13579][26])
pattern = re.compile (r"""
((0{2}?)([2468][480] | [13579][26]))
""", re.X)
when I left out
(0{2}?)
24 12 and everything works..
I'm using verbose so spacing shouldn't matter..
Invalid
12
24
28
16
EDIT :: Actually all is invalid now..
i don't understand why 24 is invalid and 28 is invalid this doesn't make sense at all. I appreciate your guidance.
Upvotes: 1
Views: 2038
Reputation: 225125
When you write (0{2}?)
, that means “match two 0
s here, but match as few as possible”. Non-greediness doesn’t really make sense for an {n}
quantifier (it does for {n,}
, and {m,n}
) – did you mean (0{2})?
?
Oh, and do keep in mind that years divisible by 400 are leap years.
Upvotes: 4
Reputation: 281683
Using the re.DEBUG
flag to show debug info about the expression, we get
>>> pattern = re.compile(r'0{2}?', re.DEBUG)
min_repeat 2 2
literal 48
The min_repeat
shows that 0{2}?
isn't being interpreted as ?
applied to 0{2}
. It's being interpreted as a lazy quantifier, attempting to match 0 any number of times from 2 to 2, but as few as possible. This doesn't quite seem consistent with the documentation; the docs only show the {m,n}?
form.
Upvotes: 3