Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^3: How does one get all possible matches from regex?

by educated_foo (Vicar)
on Dec 10, 2013 at 04:17 UTC ( #1066370=note: print w/ replies, xml ) Need Help??


in reply to Re^2: How does one get all possible matches from regex?
in thread How does one get all possible matches from regex?

They're different: /X/g does non-overlapping matches; /X(?{print$&})(?!)/ does all matches. It depends what you want.


Comment on Re^3: How does one get all possible matches from regex?
Select or Download Code
Re^4: How does one get all possible matches from regex?
by LanX (Canon) on Dec 10, 2013 at 16:27 UTC
    Well it took me a while to understand whats going on in your code...

    Please note that the while-loop is redundant (and confusing =)

    DB<138> $_=" x1x2x3x x4x5x" => " x1x2x3x x4x5x" DB<139> ; /x(\d)x(?{print "<$1>\t"})(?!)/ <1> <2> <3> <4> <5>

    by placing an _empty_ negative lookahead you are forcing your regex to reject all matches and to backtrack through the whole string.

    But the embedded perlcode (which is an experimental feature IIRC) prints the temporary match before the next attempt is started.

    edit

    as a side note, the same effect can be achieved with "conventional" means (i.e. manipulation of pos)

    DB<154> while ( /x(\d)x/g ) { print $1; pos($_) -= length($ &)-1 } 12345

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2014-09-19 14:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (140 votes), past polls