Skip to content

Commit b539fa5

Browse files
author
Donald Hunter
committed
Describe greedy vs frugal modifiers in regexes
1 parent 58c0604 commit b539fa5

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

doc/Language/regexes.pod

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,24 @@ occur between each of the matches. So, for example C<a+ % ','> will match
378378
C<a> or C<a,a> or C<a,a,a> or so on, but it will not match C<a,> or C<a,a,>.
379379
To match those as well, you may use C<%%> instead of C<%>.
380380
381+
=head2 X<Greedy versus frugal quantifiers: ! and ?>
382+
383+
By default, quantifiers request a greedy match:
384+
385+
=begin code
386+
'abababa' ~~ /a .* a/ && say ~$/; # abababa
387+
=end code
388+
389+
You can attach a C<?> modifier to the quantifier to enable frugal
390+
matching:
391+
392+
=begin code
393+
'abababa' ~~ /a .* a/ && say ~$/; # abababa
394+
'abababa' ~~ /a .*? a/ && say ~$/; # aba
395+
=end code
396+
397+
You can also explicitly request greedy matching with the C<!> modifier.
398+
381399
=head1 X<Alternation|regex,||>
382400
383401
To match one of several possible alternatives, separate them by C<||>; the

0 commit comments

Comments
 (0)