Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
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 rifling through the Monastery: (5)
As of 2014-12-20 23:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (99 votes), past polls