At 8:11 AM +0530 5/26/11, Abhinav Kukreja wrote:
hi,
suppose i have a string that contains n number of date patterns in the
string.
"2011/05/25 07:24:58 -0700 PDT 2011/04/28 XXXXXXXXXXXX 2023/23/45"
how can i extract all such patterns.
Use a regular expression that matches just those strings, use the
///g modifier (global), and either use the regular expression
repeatedly in a while loop or assign the return value(s) to an array
(or otherwise use in list context).
Here is an example (untested):
while( $string =~ m{ (\d\d\d\d/\d\d/\d\d \d\d:\d\d:\d\d [-+]\d\d\d\d
\w\w\w) }x ) {
# matched substring is in $1
}
Or this:
my @matches = ($string =~ m{ (\d\d\d\d/\d\d/\d\d \d\d:\d\d:\d\d
[-+]\d\d\d\d \w\w\w) }x );
You can, of course, modify the regular expression and make it more or
less discriminatory (and certainly shorter), but you should get the
idea.
Good luck.
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/