CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
storing match into other scalar

 



zerysaish
New User

Apr 7, 2010, 8:32 AM

Post #1 of 5 (3502 views)
storing match into other scalar Can't Post

i have $string = "We'll meet again on 20/10/2010";
$string2 = "10/10/2012 sounds better";

how can i extract those date using regex, and store them into other scalar

eg. $date1 = 20/10/2010
$date2 = 10/10/2012


roolic
User

Apr 7, 2010, 8:48 PM

Post #2 of 5 (3490 views)
Re: [zerysaish] storing match into other scalar [In reply to] Can't Post


Code
# 1 
my $date;
if( $string =~/(\d{2}\/\d{2}\/\d{4})/ ){ $date = $1; }
# 2
my ($date) = ($string =~/(\d{2}\/\d{2}\/\d{4})/g ); # first
my @dates = ($string =~/(\d{2}\/\d{2}\/\d{4})/g ); # all



zerysaish
New User

Apr 8, 2010, 8:48 AM

Post #3 of 5 (3480 views)
Re: [roolic] storing match into other scalar [In reply to] Can't Post

if($string =~ m{\b(
\d{1,2}
/
\d{1,2}
/
/d{2,4})
\b}gx)
{
@dates = ????
}

how can u store all the matches found into that @dates array? if let say $string = "01/12/1990 11/11/2000 12/31/2000"


rovf
Veteran

Apr 9, 2010, 12:52 AM

Post #4 of 5 (3473 views)
Re: [zerysaish] storing match into other scalar [In reply to] Can't Post

In your regexp, you use grouping (parentheses; see the perlre man-page) to capture the patterns you are interested in. Then you have two possibilities:

(1) If you evaluate the pattern match in list context, i.e.

@s=$x =~ /...(....)....(....)..../

the result contains the parts of the string which match the groups. This is discussed in the perlop man-page (scroll
forward to the section "Regexp Quote-Like Operators").

(2) You can pick up $1, $2 etc. after the match.


zerysaish
New User

Apr 10, 2010, 9:39 AM

Post #5 of 5 (3431 views)
Re: [rovf] storing match into other scalar [In reply to] Can't Post

thank u very much for ur assistance, i managed to find out how to do that.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives