Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: regexp to grab STRING...[everything here]...DIF STRING

by AnomalousMonk (Abbot)
on Oct 25, 2012 at 23:23 UTC ( #1000963=note: print w/ replies, xml ) Need Help??


in reply to regexp to grab STRING...[everything here]...DIF STRING

Just a cautionary example of greedy  .* versus lazy  .*? regex matching:

>perl -wMstrict -le "my $s = 'xx FOR foo IN bar baz and FOR fee fie foe IN fum yy'; ;; my @for_in = $s =~ m{ FOR .* IN }xmsg; printf 'greedy: '; printf qq{'$_' } for @for_in; print ''; ;; printf 'lazy: '; @for_in = $s =~ m{ FOR .*? IN }xmsg; printf qq{'$_' } for @for_in; " greedy: 'FOR foo IN bar baz and FOR fee fie foe IN' lazy: 'FOR foo IN' 'FOR fee fie foe IN'

Also note that in the conditional statement
    if (my @captures = $string =~ m{ pattern }xmsg) {
        do_something_with(@captures);
        }
if nothing matches (no captures, array empty), the conditional evaluates false and the statement body is not executed.


Comment on Re: regexp to grab STRING...[everything here]...DIF STRING
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (10)
As of 2015-07-30 10:40 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 (271 votes), past polls