1

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.

9
  • 4
    But why use regex for validating leap year. Commented Feb 9, 2014 at 5:15
  • Why would you ever use {1}? It does nothing. Commented Feb 9, 2014 at 5:15
  • 2
    We have a problem. "Wait, I know regex." Now we have two problems. Commented Feb 9, 2014 at 5:16
  • 1
    Can you show us runnable code that demonstrates the problem, with actual syntax and actual spacing? Commented Feb 9, 2014 at 5:18
  • 1
    Spaces in verbose mode doesn't count.. Commented Feb 9, 2014 at 5:24

2 Answers 2

4

When you write (0{2}?), that means “match two 0s 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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes that's my mistake. Thank you! What kind of mistake what am I looking for and which documentation should I look at? Thanks so much.
@LeonardLie: I don’t think this is specifically documented anywhere, but most quantifiers can have a ? appended to them to make them non-greedy. Just remember that a quantifier can’t directly follow a quantifier.
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.