[英]Using RegEx for validating First and Last names in Java
I am trying to validate a String
which contains the first & last name of a person. 我试图验证一个包含一个人的名字和姓氏的
String
。 The acceptable formats of the names are as follows. 可接受的名称格式如下。
Bruce Schneier
Schneier, Bruce
Schneier, Bruce Wayne
O’Malley, John F.
John O’Malley-Smith
Cher
I came up with the following program that will validate the String variable. 我想出了以下程序来验证String变量。 The
validateName
function should return true
if the name format matches any of the mentioned formats able. 如果名称格式与任何提到的格式匹配,则
validateName
函数应返回true
。 Else it should return false
. 否则它应该返回
false
。
import java.util.regex.*;
public class telephone {
public static boolean validateName (String txt){
String regx = "^[\\\\p{L} .'-]+$";
Pattern pattern = Pattern.compile(regx, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(txt);
return matcher.find();
}
public static void main(String args[]) {
String name = "Ron O’’Henry";
System.out.println(validateName(name));
}
}
But for some reason, it is returning false
for any value. 但由于某种原因,它对任何值都返回
false
。 What am I doing wrong here? 我在这做错了什么?
你可以做:
^[^\s]+,?(\s[^\s]+)*$
Use this: 用这个:
^[\p{L}\s.’\-,]+$
Demo: https://regex101.com/r/dQ8fK8/1 演示: https : //regex101.com/r/dQ8fK8/1
'
and '
is different. '
和'
是不同的。 You can only achieve that character by copy pasting from the text. \\-
instead of -
in []
since it will be mistaken as a range. []
使用\\-
而不是-
因为它会被误认为是一个范围。 For example: [az]
[az]
\\s
instead of \\s
代替
for matching any whitespaces. You put too many backslashes in the regex: "^[\\\\\\\\p{L} .'-]+$"
你在正则表达式中放了太多的反斜杠:
"^[\\\\\\\\p{L} .'-]+$"
After Java literal interpretation, that is: ^[\\\\p{L} .'-]+$
在Java文字解释之后,即:
^[\\\\p{L} .'-]+$
Which means match any combination of the following characters: 这意味着匹配以下字符的任意组合:
\ p { L } space . ' -
If you change to: "^[\\\\p{L} .'-]+$"
如果您更改为:
"^[\\\\p{L} .'-]+$"
Regex will see: ^[\\p{L} .'-]+$
正则表达式会看到:
^[\\p{L} .'-]+$
Which means match any combination of the following characters: 这意味着匹配以下字符的任意组合:
letters space . ' -
BUT: Don't validate names . 但是: 不要验证名称 。
See What are all of the allowable characters for people's names? 请参阅人名的所有允许字符是什么? , which leads to Personal names around the world .
,这导致世界各地的个人名称 。
In short: You can't, so don't . 简而言之: 你不能,所以不要 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.