Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^5: Determing if this regex matched

by AnomalousMonk (Archbishop)
on Aug 07, 2011 at 10:22 UTC ( [id://919055]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Determing if this regex matched
in thread Determing if this regex matched

... why is $& always null in the original example.

I think the answer is that $ (in the original example) and \z (in the example below) match after the last character in the string: that's the 'position' of the end of the string. For instance, in the string 'xa' the 'a' can match and is replaced, but after this match the match position (as returned by pos) is just beyond the end of the string, where it can match once again with no 'a', no whitespace and \z — a zero-length match!

>perl -wMstrict -le "my @strings = ('x', 'xa', 'x ', 'xa ',); ;; for my $s (@strings) { $s =~ s{ a? \s* \z } { print qq{\$& '$&' pos = }, pos($s); ''; }xmseg; print qq{\$s '$s' \n}; } " $& '' pos = 1 $s 'x' $& 'a' pos = 1 $& '' pos = 2 $s 'x' $& ' ' pos = 1 $& '' pos = 2 $s 'x' $& 'a ' pos = 1 $& '' pos = 3 $s 'x'

Update: N.B.: The /g regex modifier does not act to 'reset' $&, $1, or other match variables.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-26 07:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found