Course of Raku / Regexes and grammars / Regexes / Substitution and replacement / Exercises / Censor a word
Solution: Censor a word
Here is a possible solution to the task.
Code
my $s = 'secret secret plan';
$s ~~ s:g/secret/***/;
say $s;🦋 You can find the source code in the file censor-word.raku.
Output
*** *** planComments
The
s///operator replaces the matchedsecretwith the literal text***, written without quotes, and changes$sin place.The
:gadverb is what makes it replace both occurrences. Without it, only the firstsecretwould be censored.
Course navigation
← Censor a word | Reformat a date →