U
UToronto News
I have a question about capturing information from a regular expression
match
when i run
$string="the rain in spain falls mainly on the plain";
$searchpattern=".ain";
while ($string=~m/$searchpattern/g)
{
$position=pos($string)-length($search);
print "position=$position\n";
pos($string)=$position+1;
}
-- this searches for all occurences of $search in $string, and prints out
the starting position of each
position=8
position=17
position=28
position=43
but when i run
while (($capture)=$string=~m/($searchpattern)/g)
{
$position=pos($string)-length($searchpattern);
print "position=$position : $capture\n";
pos($string)=$position+1;
}
-- I am able to capture the match, but pos($string) is empty, resulting in
$position equal to 0-length($capture)
position=-4 : rain
When capturing the match, does the pos pointer reset? Is there a way of
using this pointer with capture?
Thanks
Larry
match
when i run
$string="the rain in spain falls mainly on the plain";
$searchpattern=".ain";
while ($string=~m/$searchpattern/g)
{
$position=pos($string)-length($search);
print "position=$position\n";
pos($string)=$position+1;
}
-- this searches for all occurences of $search in $string, and prints out
the starting position of each
position=8
position=17
position=28
position=43
but when i run
while (($capture)=$string=~m/($searchpattern)/g)
{
$position=pos($string)-length($searchpattern);
print "position=$position : $capture\n";
pos($string)=$position+1;
}
-- I am able to capture the match, but pos($string) is empty, resulting in
$position equal to 0-length($capture)
position=-4 : rain
When capturing the match, does the pos pointer reset? Is there a way of
using this pointer with capture?
Thanks
Larry