Posted on 2006-04-21
Last Modified: 2013-12-26

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,
Question by:mj62mj62
    LVL 84

    Accepted Solution

    egrep '^(19|20)[0-9][0-9](0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])$'

    Author Comment

    Impressive :)

    Thanks a million,

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Title # Comments Views Activity
    Different colored text in ComboBox without Subclassing 8 30
    Strange date stored 22 95
    notReplace  challenge 53 93
    wordappend challenge 8 45
    This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
    Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now