Swap Last, First to First Last
A regex exercise from Regexdrill. Solve it with a pattern box with live highlighting in your browser, checked against hidden test strings, and it is checked the moment you submit.
The task
Lines that are exactly Last, First become First Last. Names contain letters, apostrophes and hyphens. Leave other lines unchanged.
What this exercise teaches
^([\w'-]+), ([\w'-]+)$ with $2 $1 swaps the two names and leaves lines without a comma alone.
A hint if you get stuck
Anchor both ends, capture each name with a class that also allows ' and -, and swap them in the replacement.
This exercise is part of Pro
Pro unlocks all 52 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.
You create a free account first, so Pro is attached to it.
Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.