Home:ALL Converter>Java regex to validate a name

Java regex to validate a name

Ask Time:2010-07-16T18:39:03         Author:xdevel2000

Json Formatter

To validate names that can be

John, John Paul, etc.

I use this regex:

String regex = "[A-Z]([a-z]+|\\s[a-z]+)";

but when I do:

boolean ok = Pattern.matches(regex, "John Paul");

the matches fail?

Why? I want to use matches to validate the string as whole...

Is that regex wrong?

Author:xdevel2000,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/3263978/java-regex-to-validate-a-name
Noel M :

Paul has a capital P and your regex doesn't allow for capitalization at the start of the second word.",
2010-07-16T10:40:38
Robert Watkins :

You are going to have lots of problems with validating names - there are lots of different types of names. Consider:\n\n\nJéan-luc Picard\nCarmilla Parker-Bowles\n\nJoan d'Arc\n\nMatt LeBlanc \nChan Kong-sang (Jackie Chan's real name)\nP!nk\nLove Symbol #2\n\n\nThe easiest thing to do is to get the user to enter their name and accept it as is. If you want to break it up into personal name and family names for things such as personalisation, then I suggest you break up the input fields into two (or more) parts, or simply ask for a \"preferred name\" or \"nickname\" field.\n\nI doubt you'll find a regex that can validate all the variety of names out there - get a big set of sample data (preferably real-world) before you start trying.",
2010-07-16T11:27:29
yy