@@ -813,16 +813,29 @@ C<s/matcher/replacement/>.
813
813
C < $matcher > can be a L < Regex > , or a literal C < Str > . Non-Str matcher arguments
814
814
of type L < Cool > are coerced to C < Str > for literal matching.
815
815
816
+ = head3 Literal replacement substitution
817
+
816
818
my $some-string = "Some foo";
817
819
my $another-string = $some-string.subst(/foo/, "string"); # gives 'Some string'
818
820
$some-string.=subst(/foo/, "string"); # in-place substitution. $some-string is now 'Some string'
819
821
822
+ = head3 Closures
823
+
820
824
The replacement can be a closure:
821
825
822
826
my $i = 41;
823
827
my $str = "The answer is secret.";
824
828
my $real-answer = $str.subst(/secret/, {++$i}); # The answer to everything
825
829
830
+ = head3 Captures
831
+
832
+ If you are going to use captures, you have to use a closure like so:
833
+
834
+ say 'abc123defg'.subst(/(\d+)/, { " before $0 after " })
835
+ # OUTPUT: «abc before 123 after defg»
836
+
837
+ = head3 More Examples
838
+
826
839
Here are other examples of usage:
827
840
828
841
my $str = "Hey foo foo foo";
@@ -840,6 +853,8 @@ The C<:nth> adverb has readable English-looking variants:
840
853
say 'ooooo'.subst: 'o', 'x', :3rd; # OUTPUT: «ooxoo»
841
854
say 'ooooo'.subst: 'o', 'x', :4th; # OUTPUT: «oooxo»
842
855
856
+ = head3 Adverbs
857
+
843
858
The following adverbs are supported
844
859
845
860
= begin table
0 commit comments