Skip to content

Commit 923c2df

Browse files
committed
Reflow and clarifications refs #2887
1 parent a6cec2a commit 923c2df

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

doc/Language/regexes.pod6

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -995,8 +995,8 @@ The same grouping applies to quantifiers:
995995
/ (a b)+ /; # matches one or more sequences of 'ab'
996996
/ (a || b)+ /; # matches a string of 'a's and 'b's, except empty string
997997
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
10001000
L<Match|/type/Match> objects instead.
10011001
10021002
=head2 X«Capturing|regex,( )»
@@ -1043,14 +1043,14 @@ which, by default, don't capture.
10431043
10441044
If you do not need the captures, using non-capturing C<[ ... ]>
10451045
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
10481048
=item they make matching a bit faster.
10491049
10501050
=head2 Capture numbers
10511051
10521052
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.
10541054
10551055
The following rules are listed for the sake of completeness. When you find
10561056
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:
10751075
say ~$2; # OUTPUT: «d␤»
10761076
}
10771077
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
10791081
10801082
if 'abc' ~~ / ( a (.) (.) ) / {
10811083
say "Outer: $0"; # OUTPUT: Outer: abc
@@ -1085,13 +1087,15 @@ Captures can be nested, in which case they are numbered per level
10851087
If you need to refer to a capture from within another capture, store
10861088
it in a variable first:
10871089
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␤»
10901093
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
10951099
10961100
10971101
X<|:my>
@@ -1104,7 +1108,9 @@ regex. This can be used for debugging inside regular expressions, for instance:
11041108
say "Matched $counter lines"; # OUTPUT: «Matched 3 lines␤»
11051109
11061110
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:
11081114
11091115
=begin code
11101116
grammar HasOur {

0 commit comments

Comments
 (0)