@@ -995,8 +995,8 @@ The same grouping applies to quantifiers:
995
995
/ (a b)+ /; # matches one or more sequences of 'ab'
996
996
/ (a || b)+ /; # matches a string of 'a's and 'b's, except empty string
997
997
998
- An unquantified capture produces a L < Match|/type/Match > object. When a capture is
999
- quantified (except with the C < ? > quantifier) the capture becomes a list of
998
+ An unquantified capture produces a L < Match|/type/Match > object. When a capture
999
+ is quantified (except with the C < ? > quantifier) the capture becomes a list of
1000
1000
L < Match|/type/Match > objects instead.
1001
1001
1002
1002
= head2 X « Capturing|regex,( ) »
@@ -1043,14 +1043,14 @@ which, by default, don't capture.
1043
1043
1044
1044
If you do not need the captures, using non-capturing C < [ ... ] >
1045
1045
groups provides the following benefits:
1046
- = item they more cleanly communicate the regex intent;
1047
- = item they make it easier to count the capturing groups that do mean;
1046
+ = item they more cleanly communicate the regex intent,
1047
+ = item they make it easier to count the capturing groups that do mean and
1048
1048
= item they make matching a bit faster.
1049
1049
1050
1050
= head2 Capture numbers
1051
1051
1052
1052
It is stated above that captures are numbered from left to right. While true
1053
- in principle, this is also over simplification.
1053
+ in principle, this is also an over simplification.
1054
1054
1055
1055
The following rules are listed for the sake of completeness. When you find
1056
1056
yourself using them regularly, it's worth considering named captures (and
@@ -1075,7 +1075,9 @@ the one with the most captures determines the index of the next capture:
1075
1075
say ~$2; # OUTPUT: «d»
1076
1076
}
1077
1077
1078
- Captures can be nested, in which case they are numbered per level
1078
+ Captures can be nested, in which case they are numbered per level; level 0 gets
1079
+ to use the capture variables, but it will become a list with the rest of the
1080
+ levels behaving as elements of that list
1079
1081
1080
1082
if 'abc' ~~ / ( a (.) (.) ) / {
1081
1083
say "Outer: $0"; # OUTPUT: Outer: abc
@@ -1085,13 +1087,15 @@ Captures can be nested, in which case they are numbered per level
1085
1087
If you need to refer to a capture from within another capture, store
1086
1088
it in a variable first:
1087
1089
1088
- # !!WRONG!! The $0 refers to a capture *inside* the second capture
1089
- say "11" ~~ /(\d) ($0)/; # OUTPUT: «Nil»
1090
+ = begin code
1091
+ # !!WRONG!! The $0 refers to a capture *inside* the second capture
1092
+ say "11" ~~ /(\d) ($0)/; # OUTPUT: «Nil»
1090
1093
1091
- # CORRECT: $0 is saved into a variable outside the second capture
1092
- # before it is used inside (the `{}` is needed to update the current match)
1093
- say "11" ~~ /(\d) {} :my $c = $0; ($c)/; # OUTPUT: «「11」 0 => 「1」 1 => 「1」»
1094
- say "Matched $c"; # OUTPUT: «Matched 1»
1094
+ # CORRECT: $0 is saved into a variable outside the second capture
1095
+ # before it is used inside (the `{}` is needed to update the current match)
1096
+ say "11" ~~ /(\d) {} :my $c = $0; ($c)/; # OUTPUT: «「11」 0 => 「1」 1 => 「1」»
1097
+ say "Matched $c"; # OUTPUT: «Matched 1»
1098
+ = end code
1095
1099
1096
1100
1097
1101
X < |:my >
@@ -1104,7 +1108,9 @@ regex. This can be used for debugging inside regular expressions, for instance:
1104
1108
say "Matched $counter lines"; # OUTPUT: «Matched 3 lines»
1105
1109
1106
1110
X < |:our >
1107
- The C < :our > , similarly to L < C < our > |/syntax/our> in classes, can be used in L < Grammar|/type/Grammar > s to declare variables that can be accessed, via its fully qualified name, from outside the grammar:
1111
+ The C < :our > , similarly to L < C < our > |/syntax/our> in classes, can be used in
1112
+ L < Grammar|/type/Grammar > s to declare variables that can be accessed, via its
1113
+ fully qualified name, from outside the grammar:
1108
1114
1109
1115
= begin code
1110
1116
grammar HasOur {
0 commit comments