Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Matching with /g: Is this a bug?

by kcott (Archbishop)
on May 27, 2013 at 15:30 UTC ( [id://1035429]=note: print w/replies, xml ) Need Help??


in reply to Matching with /g: Is this a bug?

G'day morgon,

"it only prints "hubba2", because (I assume) the second match only starts where the first match matched (ie pos is not reset)."

Yes, your assumption is correct:

$ perl -Mstrict -Mwarnings -E ' my $s = <<end; hubba1 bubba hubba2 end $s =~ /bubba/gm; say pos($s); print $s =~ /(hubba\d)/gm; ' 12 hubba2

[length("hubba1\nbubba") == 12]

and agrees with the pos documentation:

"Returns the offset of where the last m//g search left off ..."

In your last statement, you appear to be contradicting your previous, correct assumption:

"... my understanding is that the behaviour in the second case is a bug or is there something I don't understand?"

The code you've posted in the third case does not give "the correct behaviour"; it gives a syntax error. You probably meant /.../ instead of s/.../ (2 instances). Assuming you did, that also agrees with the pos documentation:

"... search position is reset (usually due to match failure ..."

-- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1035429]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-26 01:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found