views:

714

answers:

3

Spent me 3 hours to get a good regex finished for parsing these 3 main possible situations.

Redden, Taylor Captain Hasting Jr.  
Redden, Taylor Hasting Jr.  
Redden, Taylor Hasting

full, l, f, m1, m2, s = /your_regex_here/.match("Redden, Taylor Captain Hasting Jr.").to_a

I want to see what other possible answers there are beside mine, in as many languages as possible. Bonus points if you can easily get just "Redden, Taylor" or even "Redden, The Greatest Captain etc Redden III"

P.S. I encourage everyone to tweet and tag if possible. Who knows, might be fun. Try and encourage your local community like Ruby Flow for me.

For the record, I'm not trying to promote or spam twitter and ruby flow, just wanna see what comes out of a collected effort.

A: 

Ruby

a,l,f,m,m2,s=/^([A-z]+),*(\s+[A-z]+)(\s+[A-z]+)*(\s+[A-z]+)(\s+[A-z]+\.*)*$/.match("Redden, Taylor Captain Hasting Jr.").to_a

A few trims, and a join and your ready to go.

taelor
-1: The class `[A-z]` matches the following chars as well: [, ], \, ^, _ and `. And what about names like "Mike O'Hara"?
Bart Kiers
That regex matches the following string: `[,,,,,,,, _ _`
Bart Kiers
A: 

Most languages base their regular expression syntax off of Perl. The variation between regexes in modern languages are pretty small. If you use RegexBuddy you can have it spit out the regex in whichever language you choose.

Jeremy Stein
+1  A: 

As far as I understand You may be sure only in first and last names. Automatic parsing other names is bad idea - they will be frequently fail.

vitaly.v.ch
+1: validating/matching dates, names, (e-mail) addresses are no jobs for regex.
Bart Kiers