Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: Arbitrary number of captures in a regular expression

by demerphq (Chancellor)
on Sep 25, 2007 at 10:44 UTC ( #640890=note: print w/ replies, xml ) Need Help??


in reply to Re: Arbitrary number of captures in a regular expression
in thread Arbitrary number of captures in a regular expression

Id do something like this myself. Except id probably not use look ahead and instead would approach it a different way. (I might even follow up with some code later if i get some time.)

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


Comment on Re^2: Arbitrary number of captures in a regular expression
Re^3: Arbitrary number of captures in a regular expression
by Sidhekin (Priest) on Sep 25, 2007 at 11:06 UTC

    Id do something like this myself. Except id probably not use look ahead and instead would approach it a different way.

    I was annoyed with the lookahead myself, but it's unlikely to be a big deal, and I could not at the time see any way to avoid it. After some thinking, however, I believe I see a way to avoid looking ahead more than once -- just include it in the first alternation, which is matched precisely once on a successful match (anchored to the beginning of the string, and the only alternation that can match there):

    my (@match) = $str =~ /(?:^foo (?=(?:m \d+ )+bar)|(?<!^)\G)m (\d+) /g;

    ... or, in the less-terse form:

    my (@match) = $str =~ / (?: ^foo\ (?= (?:m\ \d+\ )+bar) # overall match from ^foo | (?<!^) \G # or continue from not-^ ) m\ (\d+)\ # grab each digit sequence /xg;

    I think that's the best I got. Match that? :)

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!

      I dont have time to put together a working example, but what i had in mind was using while, \G and also the /gc modifier in scalar context. Maybe from that you can come up with a working example, or prove me wrong, before I get the time to do anything useful with it.

      Using the underused /gc modifier was the key point I was thinking of tho.

      Oh and to be clear I wasnt trying to say my way would be better, just different. :-)

      ---
      $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://640890]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (12)
As of 2014-08-22 17:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (161 votes), past polls