Skip to content

Commit 84a44ce

Browse files
authored
Merge pull request #2894 from tmtvl/2892-add-non-destructive-substitution-to-regex-documentation
Add the `S///` section from operators to regexes
2 parents f5e2d6d + 7925341 commit 84a44ce

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

doc/Language/regexes.pod6

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,6 +1390,22 @@ that their regular expression needs to match every piece of data in the line,
13901390
including what they want to match. Write just enough to match the data you're
13911391
looking for, no more, no less.
13921392
1393+
=head2 X«C<S///> non-destructive substitution»
1394+
1395+
say S/o .+ d/new/ with 'old string'; # OUTPUT: «new string»
1396+
S:g/« (.)/$0.uc()/.say for <foo bar ber>; # OUTPUT: «Foo␤Bar␤Ber»
1397+
1398+
C<S///> uses the same semantics as the C<s///> operator, except
1399+
it leaves the original string intact
1400+
and I<returns the resultant string> instead of C<$/> (C<$/> still being set
1401+
to the same values as with C<s///>).
1402+
1403+
B<Note:> since the result is obtained as a return value, using this
1404+
operator with the C<~~> smartmatch operator is a mistake and will issue a
1405+
warning. To execute the substitution on a variable that isn't the C<$_> this
1406+
operator uses, alias it to C<$_> with C<given>, C<with>, or any other way.
1407+
Alternatively, use the L«C<.subst> method|/routine/subst».
1408+
13931409
=head1 X<Tilde for nesting structures|regex, tilde; regex, ~>
13941410
13951411
The C<~> operator is a helper for matching nested subrules with a

0 commit comments

Comments
 (0)