In the qr/(?:(simple).*?)+/ regex, .*? is satisfied with nothing, so it's happy. Then (?:pattern)+ is satisfied with a single 'simple'. If there were more simple... sequences immediately following, greedy + would try to grab them, but there aren't, so it don't. If + is satisfied with what it has, it can't force preceding satisfied assertions to fail.
In the qr/(?:(simple).*?){3}/ regex, the {3} quantifier cannot be satisfied until it forces the preceding .*? to grab a bunch more stuff.
(I've removed the /g modifier in these examples because it just confuses the issue.)
c:\@Work\Perl\monks>perl -wMstrict -lE "my $re = qr/(?:(s \d mple).*?)+/x; my $string = 'This is a s1mple thing just a s2mple s3mple thing.'; $string =~ $re; say $&; ;; my $string2 = 'This is a s1mples2mples3mple thing'; $string2 =~ $re; say $&; ;; $re = qr/(?:(s \d mple).*?){3}/x; $string =~ $re; say $&; " s1mple s1mples2mples3mple s1mple thing just a s2mple s3mple
In reply to Re^3: Matching a regular expression group multiple times
by AnomalousMonk
in thread Matching a regular expression group multiple times
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |