Reputation: 2663
How would I create an expression that returns false if a String contains characters other than letters (accents included), hyphens, apostrophes, and single spaces? Spare me your, "What about Влади́мир Пу́тин and 豊田 章男?". I realize that it is silly to attempt to determine what is a name and what is not, but this is for educational purposes only.
Also, are there any other symbols that are commonly found in English names?
Upvotes: 1
Views: 1590
Reputation: 44328
You could try this:
private static final Pattern namePattern =
Pattern.compile("^[-' \\p{L}\\p{M}]+$");
public static boolean isValidName(String text) {
return namePattern.matcher(text).matches()
&& text.indexOf(" ") < 0
&& !text.startsWith(" ") && !text.endsWith(" ");
}
Upvotes: 1
Reputation: 789
A simple google search would give you java regex expression with the filter as you may want. But if your question is to know what conditions should be part of the filters functionality to get valid names, then there is a nice discussion here What are all of the allowable characters for people's names?
Upvotes: 0