Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^5: Perl RE; how to capture, and replace based on a block?

by Laurent_R (Parson)
on Dec 18, 2013 at 18:36 UTC ( #1067674=note: print w/ replies, xml ) Need Help??


in reply to Re^4: Perl RE; how to capture, and replace based on a block?
in thread Perl RE; how to capture, and replace based on a block?

Hi Chris

just a quick example on greedy and non greedy quantifiers, under the Perl debugger:

DB<1> $string = "foo bar foo bar foo"; DB<2> x $string =~ /(fo.*ar)/ 0 'foo bar foo bar' DB<3> x $string =~ /(fo.*?ar)/ 0 'foo bar' DB<4>
As you can see the default "*" quantifier is greedy, so that the regex tries to match as much as possible. Using the modified "*?" quantifier, the regex becomes non greedy. You will see the same type of result if you try the "+" and the "+?" quantifiers.


Comment on Re^5: Perl RE; how to capture, and replace based on a block?
Download Code
Re^6: Perl RE; how to capture, and replace based on a block?
by taint (Chaplain) on Dec 18, 2013 at 20:18 UTC
    Thanks Laurent_R for the help.

    Your example helps better explain why I got the impression Perl was not greedy, the way sed is. Most all the examples I read in the perldocs, used .*?. But my experiments, and re-training myself with Perl RE's. Seems to show, once I get out of the sed habits. Perl will be a lot more flexible, and powerful. I should have started this a l-o-o-n-g time ago. :P

    Thanks again, Laurent_R. I really appreciate it.

    --Chris

    Yes. What say about me, is true.
    

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2014-10-31 10:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (216 votes), past polls