Don't forget about names like:
- Mathias d'Arras
- Martin Luther King, Jr.
- Hector Sausage-Hausen
This should do the trick for most things:
/^[a-z ,.'-]+$/i
OR Support international names with super sweet unicode:
/^[a-zA-ZàáâäãåąčćęèéêëėįìíîïłńòóôöõøùúûüųūÿýżźñçčšžæÀÁÂÄÃÅĄĆČĖĘÈÉÊËÌÍÎÏĮŁŃÒÓÔÖÕØÙÚÛÜŲŪŸÝŻŹÑßÇŒÆČŠŽ∂ð ,.'-]+$/u
Don't forget about names like:
- Mathias d'Arras
- Martin Luther King, Jr.
- Hector Sausage-Hausen
This should do the trick for most things:
/^[a-z ,.'-]+$/i
OR Support international names with super sweet unicode:
/^[a-zA-ZàáâäãåąčćęèéêëėįìíîïłńòóôöõøùúûüųūÿýżźñçčšžæÀÁÂÄÃÅĄĆČĖĘÈÉÊËÌÍÎÏĮŁŃÒÓÔÖÕØÙÚÛÜŲŪŸÝŻŹÑßÇŒÆČŠŽ∂ð ,.'-]+$/u
You make false assumptions on the format of first and last name. It is probably better not to validate the name at all, apart from checking that it is empty.
regular expressions - Best REGEX for first/last name validation? - Salesforce Stack Exchange
Java Regex to Validate Full Name allow only Spaces and Letters - Stack Overflow
Match Full Name - Java Task (RegEx) - Q&A Mini Forum
Validating a full name (allowing ' ', '-' and '`') and not start/endwith(- or `)
Videos
You can use
^[a-zA-Z]{4,}(?: [a-zA-Z]+){0,2}$
See the regex demo
This will work with names starting with both lower- and upper-cased letters.
^- start of string[a-zA-Z]{4,}- 4 or more ASCII letters(?: [a-zA-Z]+){0,2}- 0 to 2 occurrences of a space followed with one or more ASCII letters$- end of string.
If you need to restrict the words to start with Uppercase letters, you can use
^[A-Z][a-zA-Z]{3,}(?: [A-Z][a-zA-Z]*){0,2}$
Here is my solution:
^[a-zA-Z]{3,}( {1,2}[a-zA-Z]{3,}){0,}$
- ^ --> start of string.
- [a-zA-Z]{3,} --> 3 or more character.
- ( {1,2}[a-zA-Z]{3,}){0,} --> 0 or more words with 3 or more character.
- $ --> end of string.
Yes! Let's validate some names with RegEx.
After all, we know that all people must have a first and last name, right? And no single person has more than three or four names total? And no doubt the same person will forever be identifiable by the same name?
Plus, we know that no modern culture uses patronymic naming and people in the same nuclear family must have the same last name, right?
Well, we can at least assume that people do not have single character names, right? And there are no names that use special characters, symbols, or apostrophes?
I think your choice of RegEx to validate names is missing the point: this is a huge unwieldy problem and, even if you massively restrict the scope of names you allow, you will forever suffer the risk of false negatives and you will be turning away people from other cultures and languages. In other words, I don't think that even attempting to validate names is worth your time.
The reason it's breaking on names like McGowan is because you're second character class doesn't allow for Capitalized characters.
Use the below regex to match names with Capitalization after the first character.
([A-Z][a-zA-Z]*)
What about:
- Peter Müller
- François Hollande
- Patrick O'Brian
- Silvana Koch-Mehrin
Validating names is a difficult issue, because valid names are not only consisting of the letters A-Z.
At least you should use the Unicode property for letters and add more special characters. A first approach could be e.g.:
String regx = "^[\\p{L} .'-]+$";
\\p{L} is a Unicode Character Property that matches any kind of letter from any language/script. \\p{L} is equivalent to \\p{javaLetter}, and both are equivalent to Character::isLetter.
try this regex (allowing Alphabets, Dots, Spaces):
"^[A-Za-z\s]{1,}[\.]{0,1}[A-Za-z\s]{0,}$" //regular
"^\pL+[\pL\pZ\pP]{0,}$" //unicode
This will also ensure DOT never comes at the start of the name.