Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^4: Regex Greed

by Athanasius (Abbot)
on Aug 08, 2012 at 02:42 UTC ( #986115=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Regex Greed
in thread Regex Greed

A search on “overlapping matches’ in perldoc doesn’t turn up anything relevant. However, I did find the following in the Camel Book (4th Edition, pages 247–8, underlining added):

Lookahead assertions can be used to implement overlapping matches. For example,
"0123456789" =~ /(\d{3})/g
returns only three strings: 012, 345, and 678. By wrapping the capture group with a lookahead assertion:
"0123456789" =~ /(?=(\d{3}))/g
you now retrieve all of 012, 123, 234, 345, 456, 567, 678, and 789. This works because this tricky assertion does a stealthy sneakahead to run up and grab what’s there and stuff its capture group with it, but being a lookahead, it reneges and doesn’t technically consume any of it. When the engine sees that it should try again because of the /g, it steps one character past where last it tried.

HTH,

Athanasius <°(((><contra mundum


Comment on Re^4: Regex Greed
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2015-07-03 13:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (53 votes), past polls