Course of Raku / Regexes and grammars / Regexes / Captures / Exercises / Last name first
Solution: Last name first
Here is a possible solution to the task.
Code
if 'Grace Hopper' ~~ / (\w+) ' ' (\w+) / {
say "$1, $0";
}🦋 You can find the source code in the file swap-words.raku.
Output
Hopper, GraceComments
The first and last names are captured into
$0and$1.Inside a double-quoted string each capture interpolates as its matched text. Printing
"$1, $0"puts the last name first, followed by a literal comma and space and then the first name.