Course of Raku / Regexes and grammars / Regexes / Substitution and replacement / Exercises / Reformat a date

Solution: Reformat a date

Here is a possible solution to the task.

Code

my $d = '2025-06-15';
$d ~~ s/ (\d+) '-' (\d+) '-' (\d+) /$2\/$1\/$0/;
say $d;

🦋 You can find the source code in the file reformat-date.raku.

Output

15/06/2025

Comments

  1. The pattern captures the year into $0, the month into $1, and the day into $2.

  2. The replacement writes them in the new order $2/$1/$0, with the slashes escaped as \/. The replacement side has no surrounding spaces, because spaces there would be inserted literally.

Course navigation

Reformat a date   |   Lower-case letters