Skip to content

Commit 198f0ac

Browse files
authored
Merge pull request #2286 from MorayJ/regex-clarity
Clarifies capturing properties of square brackets in regex
2 parents 7913c42 + f596e21 commit 198f0ac

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

doc/Language/regexes.pod6

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,7 @@ The parentheses in regexes perform a double role: they group the regex
10081008
elements inside and they capture what is matched by the sub-regex inside.
10091009
10101010
To get only the grouping behavior, you can use square brackets C<[ ... ]>
1011-
instead.
1011+
which, by default, don't capture.
10121012
10131013
if 'abc' ~~ / [a||b] (c) / {
10141014
say ~$0; # OUTPUT: «c␤»
@@ -1103,9 +1103,16 @@ and slightly verbose, way of naming captures is like this:
11031103
say ~$<myname> # OUTPUT: «abc␤»
11041104
}
11051105
1106+
The square brackets in the above example, which don't usually capture, will now capture its grouping
1107+
with the given name.
1108+
11061109
The access to the named capture, C«$<myname>», is a shorthand for indexing
11071110
the match object as a hash, in other words: C<$/{ 'myname' }> or C«$/<myname>».
11081111
1112+
We can also use round brackets in the above example, but they will work exactly the same as square brackets.
1113+
The captured group will only be accessible by its name as a key from the match object and not from
1114+
its position in the list with C<$/[0]> or C<$0>.
1115+
11091116
Named captures can also be nested using regular capture group syntax:
11101117
11111118
if 'abc-abc-abc' ~~ / $<string>=( [ $<part>=[abc] ]* % '-' ) / {
@@ -1129,7 +1136,7 @@ all named captures:
11291136
}
11301137
}
11311138
1132-
A more convenient way to get named captures is discussed in
1139+
A more convenient way to get named captures is by using named regex as discussed in
11331140
the L<Subrules|#Subrules> section.
11341141
11351142
=head2 X«Capture markers: C«<( )>»|regex,<( )>»

0 commit comments

Comments
 (0)