I'm trying to write a pretty simple perl regex to match dates like these: (and capture the year and month)

2006-04
2007-12
2008-9

that is: four digit year followed by a hyphen, and then a 1 or 2 digit month with or without a leading 0.

So far I've got this:

/^(\d{4})-([01]?\d{1})$/

but it matches months up to 19. It needs to somehow know not to capture a second digit in the month past 2 if the first digit is a 1.

Any help with this is greatly appreciated.

    /^(\d{4})-(0?\d|1[0-2])$/
    

      Ahhh. Thanks! That's perfect.

        Write a Reply...