Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Dynamic regex assertions, capturing groups, and parsers: joy and terror

by demerphq (Chancellor)
on Oct 03, 2005 at 22:32 UTC ( #497068=note: print w/replies, xml ) Need Help??


in reply to Dynamic regex assertions, capturing groups, and parsers: joy and terror

I find this quite confusing as well.

Update: No this makes perfect sense. Thanks to dio for straightening me out.

$rx = qr{ (.) (??{ print $1 }) }x; print "!" if "japhy" =~ $rx; __END__ japhy

How does $1 end up being 'japhy' with this re? Interestingly, changing it to

$rx = qr{ (.) (??{ print $1; '' }) }x; print "!" if "japhy" =~ $rx; __END__ j!

makes things work out properly.

---
$world=~s/war/peace/g

Replies are listed 'Best First'.
Re^2: Dynamic regex assertions, capturing groups, and parsers: joy and terror
by diotalevi (Canon) on Oct 03, 2005 at 23:09 UTC

    The result of (??{ print $1 }) is 1 because print() succeeded in writing to STDOUT. The regex that was then compiled by (??{ ...}) was "1" which then failed. So the (.) advanced over every character and printed them individually. The proper thing to do here would have been (?{ ... }) which will not affect regex matching.

      Doh. Of course. I knew that the print returning 1 failed the match, but i didn't put two and two together to realize that was why all of the chars were printed. And I've used this technique deliberately before too. /gah.

      Thanks for the clue-by-four. :-)

      ---
      $world=~s/war/peace/g

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2020-05-31 13:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (173 votes). Check out past polls.

    Notices?