in reply to Re: regex for multiple dates
in thread regex for multiple dates

Thanks to both of you, I know my code was hard to read. I'm new regex and having a very hard time understanding the syntax and writing the code so that I can go back and understand what I wrote.

The text I get has one or two dates in it and the format is different in it may not have the year, I didnt know how to make a portion of the check optional.

By looking at the code you guys wrote I think I see what I'm doing wrong, I'm rewriting the code. thanks for the help

Replies are listed 'Best First'.
Re^3: regex for multiple dates
by Jim (Curate) on Jul 07, 2011 at 05:14 UTC

    I think it's often helpful to start with zero generality. Figure out the tricky bits of the match operation and regular expression pattern first, then generalize the pattern.

    Let's assume you want to match one or more of these specific timestamps in a string:

        Mar 11 08:02:08
        Mar 11 08:02:08.32
        Mar 11 2011 08:02:08
        Mar 11 2011 08:02:08.32
    

    This expression will match and capture them:

    m/(Mar 11 (?:2011 )?08:02:08(?:\.32)?)/g;

    Simple.

    Now it's easy to generalize this pattern as much as you need to for your specific application. The following generalization is probably sufficient:

    m{((?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+ \d\d?\s+(?:(?:19|20)\d\d\s+)?\d\d:\d\d:\d\d(?:\.\d\d)?)}gx;

    Jim