egrep YYYYMMDD

Hello,

Does anyone have an egrep regular expression (regex) for valid dates in the format YYYYMMDD?

It's fairly simple for basic checking, for example the DD bit could be:
[0-3][0-9] but that doesn't catch invalid days like 38.

if I write a full regex, starting with ^ and ending in $, I can then use the | (the OR option) along with a second full regex to capture the two cases of [0-2][0-9] and [3][0-1], however that is quite long. (ie. ^<full regex>$|^<second full regex>$)

Ideally I'd like to be able to do an embedded OR within a single regex to capture that 29 is a valid day by 39 is not.  But I can't figure out how to do that.

Of course, something that captured leap years and fancy stuff like that would be awesome, but I'm not holding my breath on it. :)

Thank You,
MJ
mj62mj62Asked:
Who is Participating?
 
ozoCommented:
egrep '^(19|20)[0-9][0-9](0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])$'
0
 
mj62mj62Author Commented:
Impressive :)

Thanks a million,
MJ
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.