4.18. Reformat Names From “FirstName LastName” to “LastName, FirstName”
Problem
You want to convert people’s names from the “FirstName LastName” format to “LastName, FirstName” for use in an alphabetical listing. You additionally want to account for other name parts, so that you can, say convert “FirstName MiddleNames Particles LastName Suffix” to “LastName, FirstName MiddleNames Particles Suffix.”
Solution
Unfortunately, it isn’t possible to reliably parse names using a regular expression. Regular expressions are rigid, whereas names are so flexible that even humans get them wrong. Determining the structure of a name or how it should be listed alphabetically often requires taking traditional and national conventions, or even personal preferences, into account. Nevertheless, if you’re willing to make certain assumptions about your data and can handle a moderate level of error, a regular expression can provide a quick solution.
The following regular expression has intentionally been kept simple, rather than trying to account for edge cases.
Regular expression
^(.+?)●([^\s,]+)(,?●(?:[JS]r\.?|III?|IV))?$
| Regex options: Case insensitive |
| Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby |
Replacement
$2,●$1$3| Replacement text flavors: .NET, Java, JavaScript, Perl, PHP |
\2,●\1\3| Replacement text flavors: Python, Ruby |
JavaScript example
function formatName(name) {
return name.replace(/^(.+?) ([^\s,]+)(,? (?:[JS]r\.?|III?|IV))?$/i,
"$2, $1$3");
}Recipe 3.15 has code listings that will help ...