In it, I found this paragraph:
String.match(pattern)This wasn't exactly what I wanted because I wanted capture parenthesis. I opened up Firebug and entered these 2 lines (And yes, there are more efficient ways to parse out dates, I'm just using this as an example):
var str = "Watch out for the rock!".match(/r?or?/g)
str then contains ["o","or","ro"]
This showed me that my variable testArray will hold the full match (without my matching groups), and then each capture group in the rest of the elements in the array.>>> var testArray = "2009-01-01".match(/(\d{4})-(\d{2})-(\d{2})/)>>> testArray["2009-01-01", "2009", "01", "01"]
I think the equivalent of this in Perl is something like this (although I don't have Perl handy to test this):
my $date='2009-01-01';I could have just assigned @testArray to the outcome of the regex match, but Perl doesn't put the full match into that array. The $& variable in Perl contains the entire matched string.
my @testArray;
if ( $date =~ /(\d{4})-(\d{2})-(\d{2})/ ) {
@testArray = ($&, $1, $2, $3);
}
No comments:
Post a Comment