Why always Regex? Maybe, more simple solutions is:
string[] names = fullName.Split(
new char[] { '.', ' ' },
System.StringSplitOptions.RemoveEmptyEntries);
After that, provide processing depending on
names.Length
.
[EDIT]
I agree with criticism I got from Manfred. My idea was yet another, simpler "solution" in case the bigger problem is so ill-posed.
Here is how it looks. You receive one single string from the user; I called it
fullName
. You split it, but what you can do with the result? You would classify results in cases where there is middle name, only a middle initial, no middle name, etc.? This is all invalid in a little more general case, anyway.
I can tell you that the Western (mostly English language based schema) "First Name — Middle Name — Last Name" is only applicable within (maybe some, not even all) English-speaking countries, and not very robust even in those countries.
Applying this is international level is also used, but this is
purely idiotic!
Did you know, that what called "first name" in absolute majority if earth population means "family name", not "given name"?! In Russian, "middle name" is always a patronymic; and the forms of all three parts of the name are gender-modified (also depend on plurality and grammatical case). In many cultures more than three names is used to conduct the named identity of a person, in other cultures, there no family name at all. The English naming schema does not work well even for Europe.
The whole idea of extrapolating this English naming schema to the whole world is often applied and is
purely idiotic.
Regex will not help here as well (that's why I came up with simpler solution: "it all is so bad anyway…").
This issue cannot have satisfactory resolution based on any means beyond the different use-case design. The whole idea should be given up. (And don't tell me about "requirements"!)
The name provided by the user should either be used as a non-breaking entity or the classification of the component or such names should be provided by the users: a different field for each name component, with the selection of "classifier".
By the way, think about what the idea plays with. This is no better way to offense a person than messing up one's name. This is a matter of simple politeness.
[END EDIT]
—SA