Reputation: 2293
I'm trying to write a regular expression that swaps lastname, firstname middleinitial to firstname lastname.
so, for example
doe, john j
another example:
doe, jane
becomes
john doe
2nd example results
jane doe
I've tried this: (\w+?), (.+?)(&|-) ((\w+?),(.+?)(&|-))*(.+)
and this: ^(\w+), *([\w \.]+)[ ]+-[ ]*(.*)
Neither work.
Any help would be appreciated.
Thanks,
Upvotes: 1
Views: 2026
Reputation: 48827
Replacing ^([a-zA-Z]+?),\s*([a-zA-Z]+?)(\s+[a-zA-Z])?$
by $2 $1
should suit your needs.
Note that I didn't use the \w
class since it matches underscores (_
) and digits too, and I've never seen someone whose name is John_ D0e
yet ;)
Upvotes: 1
Reputation: 83
May be this might help:
perl -e '$name = "doe, john j"; $name =~ /(.*?), (.*)/is; $fname = $2; $lname = $1; $fname =~ s/\s.*//is; $name = $fname. " " . $lname; print "$name\n";'
Upvotes: 0