Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: regex for multiple dates

by Somni (Friar)
on Jul 06, 2011 at 23:43 UTC ( #913102=note: print w/replies, xml ) Need Help??

in reply to regex for multiple dates

Can't say I dig your regex all that much. Too many uses of unnecessary brackets, and an avoidance of more readable character classes. It's good you started with /x, but there is such thing as too much vertical whitespace.

There are a few ways of accomplishing your task, depending on what you want from the result. There's yours, where everything is jammed into one regex; this is ideal if you don't need anything beyond that (say, to capture the individual pieces, or easily associate something with which pattern matched). For just pulling dates out, it typically works.

However, when matching multiple possible patterns I tend to go with an incremental parser. You define all of your patterns, and organize them such that the more exact match first. For example, with your data set:

my $month_abv = qr/[[:upper:]][[:lower:]]{2}/; my @date_patterns = ( # Mar 11 2011 08:02:08.32 ['Mmm DD YYYY HH:MM:SS.uu', qr/$month_abv \s+ \d\d? \s+ \d{4} \s+ \d\d:\d\d:\d\d\.\d\d /x], # Mar 11 2011 08:02:08 ['Mmm DD YYYY HH:MM:SS', qr/$month_abv \s+ \d\d? \s+ \d{4} \s+ \d\d:\d\d:\d\d /x], # Mar 11 08:02:08.32 ['Mmm DD HH:MM:SS.uu', qr/$month_abv \s+ \d\d? \s+ \d\d:\d\d:\d\d\.\d\d /x], # Mar 11 08:02:08 ['Mmm DD HH:MM:SS', qr/$month_abv \s+ \d\d? \s+ \d\d:\d\d:\d\d /x], ); my $line = q{ Mar 11 08:02:08 Mar 11 2011 08:02:08 D+R-FW-1 : Jan 1 2011 00:00:00.07; }; DATE: until ($line =~ /\G\z/gc) { foreach my $pat (@date_patterns) { my($form, $re) = @$pat; if ($line =~ /\G\b($re)\b/gc) { say "Matching date form '$form': $1"; next DATE; } } $line =~ /./sgc; # no matches, bumpalong }

Each of your possible patterns are defined (in length order, so that the smaller patterns are tried last). You get free metadata with it; in this case, a simple string that describes the pattern. In more complicated cases you'd want a function that reformats the date, or shoves it into an object, etc. based on captures within the pattern.

Incidentally, while I did change your [A-Z] and [a-z] to something a little more locale friendly ([[:upper:]] and [[:lower:]]), what the month pattern really should be is simply \w+, or perhaps [[:alpha:]]. For your data set it probably doesn't matter, but not all languages provide three characters for the month, or provide it with the first letter uppercased. Some locales may not even use all letters for month names. The rest of the pattern in each case should filter out false positives.

In addition to changing the metadata, you could change the \b delimiters; perhaps your dates are all before and after whitespace, or some punctuation. Making it specific to your data set can eliminate more false positives.

In order to accomplish this with one big regex you would simply join the regexes in @date_patterns with |, and commence matching as normal.

Replies are listed 'Best First'.
Re^2: regex for multiple dates
by Anonymous Monk on Jul 07, 2011 at 01:52 UTC

    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

      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;


      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;


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://913102]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2023-11-30 06:51 GMT
Find Nodes?
    Voting Booth?

    No recent polls found