in reply to How to modify my regex?
What is wrong with my regex?
It's too greedy. Make .* less greedy by adding the ? "lazy" modifier to the * "zero-or-more" quantifier to make it .*? instead.
Update: See Quantifiers in perlre; also see discussions of greedy/lazy matching in perlretut. (Update: See also Quantifiers in regular expressions in Tutorials.)
Update 2: Example:
c:\@Work\Perl\monks\OldChamp>perl -wMstrict -le "my $s = 'keep me ZIP delete this ZAP also keep this ZIP kill too ZAP +keep too'; print qq{'$s'}; ;; (my $t = $s) =~ s{ ZIP .* ZAP }{}xmsg; print qq{greedy .*: '$t'}; ;; ($t = $s) =~ s{ ZIP .*? ZAP }{}xmsg; print qq{lazy .*?: '$t'}; " 'keep me ZIP delete this ZAP also keep this ZIP kill too ZAP keep too' greedy .*: 'keep me keep too' lazy .*?: 'keep me also keep this keep too'
Give a man a fish: <%-{-{-{-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to modify my regex?
by OldChamp (Acolyte) on Aug 26, 2015 at 20:43 UTC | |
by AnomalousMonk (Archbishop) on Aug 26, 2015 at 23:10 UTC | |
by OldChamp (Acolyte) on Aug 27, 2015 at 16:50 UTC | |
by AnomalousMonk (Archbishop) on Aug 28, 2015 at 17:12 UTC |