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

*** *** plan

Comments

  1. The s/// operator replaces the matched secret with the literal text ***, written without quotes, and changes $s in place.

  2. The :g adverb is what makes it replace both occurrences. Without it, only the first secret would be censored.

Course navigation

Censor a word   |   Reformat a date