pos and capture

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
 
L

LEH

I sorted out how to get this working.
instead of capturing into a variable, I capture into the $1 and use that

ie
while($string=~/($searchpatten)/g)
{
$position=pos($string)-length($searchpattern);
print "position=$position: $1\n";
pos($string)=$position+1;
}

this does work, but I am still curious to know why my capturing into a
variable resets pos($string)

Larry
 
J

Jeff 'japhy' Pinyan

[posted & mailed]

this does work, but I am still curious to know why my capturing into a
variable resets pos($string)

The problem is that saying

(...) = $string =~ /pattern/g;

enforces *list* context on the pattern match, and a global pattern match
in list context matches as many times as possible, and afterwards, pos()
doesn't have a useful value.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
RPI Corporation Secretary % have long ago been overpaid?
http://japhy.perlmonk.org/ %
http://www.perlmonks.org/ % -- Meister Eckhart
 
A

Anno Siegel

UToronto News said:
I have a question about capturing information from a regular expression
match

when i run

....without strictures and warnings, it seems.
$string="the rain in spain falls mainly on the plain";
$searchpattern=".ain";

while ($string=~m/$searchpattern/g)
{
$position=pos($string)-length($search);
^^^^^^^
There is no variable "$search". "strict" would have told you so.
Also, the length of the search pattern is generally not the same
as the length of the match. Use the @- array for the beginning
of a match.
print "position=$position\n";
pos($string)=$position+1;

This is an error. pos( $string) points already one character past
the match. Incrementing it further may miss some.
}

-- 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

Have you checked that? Your positions are too large by 4:

print "position=$-[ 0]\n" while $string =~ /$searchpattern/g;

position=4
position=13
position=24
position=39

Your question about pos() after a match in list context has been answered.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top