File tree Expand file tree Collapse file tree 1 file changed +24
-1
lines changed Expand file tree Collapse file tree 1 file changed +24
-1
lines changed Original file line number Diff line number Diff line change @@ -408,7 +408,30 @@ the capturing groups that you do care about, and it is a bit faster.
408
408
409
409
= head2 Capture Numbers
410
410
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
+ }
412
435
413
436
= head1 Adverbs
414
437
You can’t perform that action at this time.
0 commit comments