用于验证全名的Java Regex仅允许空格和字母 [英] Java Regex to Validate Full Name allow only Spaces and Letters
问题描述
我希望正则表达式只验证字母和空格。基本上这是验证全名。例如:史蒂夫柯林斯先生或史蒂夫柯林斯我试过这个正则表达式。 [1 - ZA-Z] + \?但没有工作。有人可以帮助我吗
p.s.我用的是Java。
I want regex to validate for only letters and spaces. Basically this is to validate full name. Ex: Mr Steve Collins or Steve Collins I tried this regex. "[a-zA-Z]+\.?" But didnt work. Can someone assist me please p.s. I use Java.
public static boolean validateLetters(String txt) {
String regx = "[a-zA-Z]+\\.?";
Pattern pattern = Pattern.compile(regx,Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(txt);
return matcher.find();
}
推荐答案
怎么样? :
- PeterMüller
- FrançoisHollande
- Patrick O'Brian
- Silvana Koch-Mehrin
- Peter Müller
- François Hollande
- Patrick O'Brian
- Silvana Koch-Mehrin
验证名称是一个难题,因为有效名称不仅包含字母AZ。
Validating names is a difficult issue, because valid names are not only consisting of the letters A-Z.
至少应该使用字母的Unicode属性并添加更多特殊字符。第一种方法可以是例如:
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}
是 Unicode字符属性,可以匹配来自任何语言的任何类型的字母
\\p{L}
is a Unicode Character Property that matches any kind of letter from any language
这篇关于用于验证全名的Java Regex仅允许空格和字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!