Blog Archive

Monday, July 1, 2019

Perl regular expression: Greedy and Non-Greedy Quantification

source: https://www.itworld.com/article/2786107/regular-expression-tutorial-part-5--greedy-and-non-greedy-quantification.html

perl search in greedy mode:
my $string = 'bcdabdcbabcd';
$string =~ m/^(.*)ab/;
print "$1\n"; # prints: bcdabdcb

perl search in non-greedy mode:
my $string = 'bcdabdcbabcd';$string =~ m/^(.*?)ab/;
print "$1\n"; # prints: bcd
In this case the .*? portion attempts to match the least amount of data

No comments:

Post a Comment