Skip to content

Commit e3fec75

Browse files
committed
Elaborate on capture numbering
1 parent 775f199 commit e3fec75

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

lib/Language/regexes.pod

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,30 @@ the capturing groups that you do care about, and it is a bit faster.
408408
409409
=head2 Capture Numbers
410410
411-
TODO: describe how alternations affect capturing numbers; nested captures
411+
The statement above that captures are numbered from left to right. While true
412+
in principle, it is also overly simplistic.
413+
414+
The following rules are listed for the sake of completeness; when you find
415+
yourself using them regularly, it is worth considering named captures (and
416+
possibly subrules) instead.
417+
418+
Alternations resets the capture count:
419+
420+
/ (x) (y) || (a) (.) (.) /
421+
# $0 $1 $0 $1 $2
422+
423+
Example:
424+
425+
if 'abc' ~~ /(x)(y) || (a)(.)(.)/ {
426+
say ~$1; # b
427+
}
428+
429+
Captures can be nested, in which case they are numbered per level
430+
431+
if 'abc' ~~ / ( a (.) (.) ) / {
432+
say "Outer: $0"; # Outer: abc
433+
say "Inner: $0[0] and $0[1]"; # Inner: b and c
434+
}
412435
413436
=head1 Adverbs
414437

0 commit comments

Comments
 (0)