I would like some help parsing a string of someones name. I would like to take the string and remove it so that only the first name will be in the string.
Suppose I have a name like this
Mr. John Doe
John Smith.
In both cases I would like to only get the first name of the string and delete all other characters.
So for both strings after they have been parsed will on have John in them
I was wondering if there was a way to do this problem with regex.
You can’t do it. Not without annoying some people because you’ve mangled their name. You can’t distinguish between John Paul Doe (first name “John”, middle name “Paul”, last name “Doe”), John Joseph Brown (answers to “Joseph” or “Joe” but only ever uses “John” on government forms), and John Paul Smith (first name “John Paul” and hates to have it shortened).
Go read Falsehoods Programmers Believe About Names.
Then go read it again, and this time accept that, yes, 95% of the population of the world have
a completely different idea of what a name is from yours. (80% if you’re Chinese.)
The most you can do is to truncate whitespace and maybe a few “safe” prefixes like “Mr” — and I wouldn’t do even that (if people bothered in to write “Mr”, they presumably like to have it there).