Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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

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

in reply to How does one get all possible matches from regex?

Your "correct results" don't correspond to what I get; if you want "all possible matches for REGEX," you should use this:
1 while /REGEX(?{print $&})(?!)/;
i.e. "match REGEX, print what it matched, then fail."

Replies are listed 'Best First'.
Re^2: How does one get all possible matches from regex?
by LanX (Chancellor) on Dec 10, 2013 at 03:38 UTC
    I didnt understand the OP, but is this not easier?

     print $1 while /(REGEX)/g

    Cheers Rolf

    ( addicted to the Perl Programming Language)

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


        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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1066363]
[runrig]: Total Eclipse
[runrig]: (of the Sun)
[runrig]: Is happening now...will get to about 70% where I am
[LanX]: enjoy :)

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2017-08-21 17:01 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (324 votes). Check out past polls.