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



zerysaish
New User

Apr 7, 2010, 8:32 AM


Views: 6704
storing match into other scalar

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


Views: 6692
Re: [zerysaish] storing match into other scalar


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


Views: 6682
Re: [roolic] storing match into other scalar

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


Views: 6675
Re: [zerysaish] storing match into other scalar

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


Views: 6633
Re: [rovf] storing match into other scalar

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