Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I am writing a regex to find mutiple dates in a string if they exist, I have two formats and I dont know if there will one or two dates ahead of time When I run this I get the match for the first date NOT the second
The date formats can be:
Mar 11 08:02:08
Mar 11 08:02:08.32
Mar 11 2011 08:02:08
Mar 11 2011 08:02:08.32
$current_line = " Mar 11 08:02:08 172.28.17.253 Mar 11 2011 08:02:08 D +R-FW-1 :"; $current_line =~ m{ ( [A-Z]{1}[a-z]{2} #Month, 1 upper w\2 lower char [ ][ ]*? #one or more spaces [0-9][0-9]? #Day, one or more numbers [ ][ ]*? #one or more spaces [0-9]{2} #Hour, 2 digits [:] #delimeter, colon [0-9]{2} #Minute, 2 digits [:] #delimeter, colon [0-9]{2} #Seconds, 2 digits [.]? #decimal point if it exists [0-9]*? #Fracion of seconds if it exists )? #string may exist more than once .*? #Any text ( [A-Z]{1}[a-z]{2} #Month, 1 upper w\ 2lower char [ ][ ]*? #one or more spaces [0-9][0-9]? #Day, one or more numbers [ ][ ]*? #one or more spaces [0-9]{4} #Year, 4 digits [ ][ ]*? #one or more spaces [0-9]{2} #Hour, 2 digits [:] #delimeter, colon [0-9]{2} #Minute, 2 digits [:] #delimeter, colon [0-9]{2} #Seconds, 2 digits [.]? #decimal point if it exists [0-9]*? #Fraction of seconds if it exists )? #Date may exist more than once }gxms; if($1){ print "Date_1 = $1\n"}; if($2){ print "Date_2 = $2\n"};
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: regex for multiple dates
by muba (Priest) on Jul 06, 2011 at 23:22 UTC | |
|
Re: regex for multiple dates
by AnomalousMonk (Archbishop) on Jul 07, 2011 at 01:51 UTC | |
|
Re: regex for multiple dates
by Somni (Friar) on Jul 06, 2011 at 23:43 UTC | |
by Anonymous Monk on Jul 07, 2011 at 01:52 UTC | |
by Jim (Curate) on Jul 07, 2011 at 05:14 UTC | |
|
Re: regex for multiple dates
by locked_user sundialsvc4 (Abbot) on Jul 07, 2011 at 02:50 UTC |