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.
In reply to Re: regexp to grab STRING...[everything here]...DIF STRING
by AnomalousMonk
in thread regexp to grab STRING...[everything here]...DIF STRING
by jeffrgsf
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |