Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^4: Determing if this regex matched

by cormanaz (Chaplain)
on Aug 06, 2011 at 20:34 UTC ( #918995=note: print w/replies, xml ) Need Help??


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

Well good point.

I still wonder why is $& always null in the original example.

Replies are listed 'Best First'.
Re^5: Determing if this regex matched
by AnomalousMonk (Chancellor) on Aug 07, 2011 at 10:22 UTC
    ... 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
Node Status?
node history
Node Type: note [id://918995]
help
Chatterbox?
[1nickt]: choroba Updated the PR, using Config instead of exposing the hackery. Using cpanfile-dump does not work to test, as shown in the earlier PR comment, even when using a binary built for a particular Perlbrew perl, the output seems to be
[1nickt]: ... from the system perl binary. Using cpanm --installdeps . does use the expected perl and tus the check acts correctly.
[choroba]: Nice, thanks. I'll look into it later, about to leave ATM.
[1nickt]: Aye-aye cap'n 🖖

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2017-10-18 16:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (249 votes). Check out past polls.

    Notices?