Getting What is Matched in Perl
To get what is matched:
$ji="London";
if ($ji =~ m/(Lon)/) {
print "$1";
}
Which Prints:
Lon
-We created a variable named $ji and stored London there.
-We put parenthesis around what we wanted to match
Lon
-The parenthesis are stored in the built-in variable $1
if ($ji =~ m/(Lon(don))/) {
print "$1 $2";
}
Which Prints:
London don
-Whatever matches the first set of parenthesis is stored in $1
-The second set is stored in $2 and so on...
Copy and Paste Perl Code:
#!/usr/bin/perl
print "Content-type:text/html\n\n";
$ji="London";
if ($ji =~ m/(Lon(don))/) {
print "$1 $2";
}
exit;
Resources:
Matching with a Regular Expression, REGEX, in Perl
Click HERE to comment or discuss at iLoveTheCode GOOGLE Group