Skip to content

Commit b5a674d

Browse files
committed
Document regex substitution adverbs
Closes #1133
1 parent ecbe324 commit b5a674d

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

doc/Language/regexes.pod6

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,6 +1426,56 @@ abracadabra
14261426
abra
14271427
=end code
14281428
1429+
=head2 Substitution Adverbs
1430+
1431+
You can apply matching adverbs (such as C<:global>, C<:pos> etc.) to
1432+
substitutions. In addition, there are adverbs that only make sense for
1433+
substitutions, because they transfer a property from the matched string
1434+
to the replacement string.
1435+
1436+
=head3 X<Samecase|subsitution adverb,:samecase;substitution adverb,:ii>
1437+
1438+
The C<:samecase> or C<:ii> substitution adverb implies the
1439+
C<:ignorecase> adverb for the regex part of the substitution, and in
1440+
addition carries the case information to the replacement string:
1441+
1442+
=begin code
1443+
$_ = 'The cat chases the dog';
1444+
s:global:samecase[the] = 'a';
1445+
say $_; # OUTPUT: «A cat chases a dog»
1446+
=end code
1447+
1448+
Here you can see that the first replacement string C<a> got capitalized,
1449+
because the first string of the matched string was also a capital
1450+
letter.
1451+
1452+
=head3 X<Samemark|subsitution adverb,:samemark;substitution adverb,:mm>
1453+
1454+
The C<:samemark> or C<:mm> adverb implies C<:ignoremark> for the regex,
1455+
and in addition, copies the markings from the matched characters to the
1456+
replacement string:
1457+
1458+
=begin code
1459+
given 'äộñ' {
1460+
say S:mm/ a .+ /uia/; # OUTPUT: «üị̂ã»
1461+
}
1462+
=end code
1463+
1464+
=head3 X<Samespace|subsitution adverb,:samespace;substitution adverb,:ss>
1465+
1466+
The C<:samespace> or C<:ss> substitution modifier implies the
1467+
C<:sigspace> modifier for the regex, and in addition, copies the
1468+
whitespace from the matched string to the replacement string:
1469+
1470+
=begin code
1471+
say S:samespace/a ./c d/.perl given "a b"; # OUTPUT: «"c d"»
1472+
say S:samespace/a ./c d/.perl given "a\tb"; # OUTPUT: «"c\td"»
1473+
say S:samespace/a ./c d/.perl given "a\nb"; # OUTPUT: «"c\nd"»
1474+
=end code
1475+
1476+
The C<ss/.../.../> syntactic form is a shorthand for
1477+
C<s:samespace/.../.../>.
1478+
14291479
=head1 Look-around assertions
14301480
14311481
=head2 X<Lookahead assertions|regex,before;regex,after>

0 commit comments

Comments
 (0)