用于验证名称的 Java 正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3263978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:03:37  来源:igfitidea点击:

Java regex to validate a name

javaregex

提问by xdevel2000

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?

那个正则表达式错了吗?

回答by Robert Watkins

You are going to have lots of problems with validating names - there are lots of different types of names. Consider:

您将在验证名称时遇到很多问题 - 有很多不同类型的名称。考虑:

  • Jéan-luc Picard
  • Carmilla Parker-Bowles
    • Joan d'Arc
  • Matt LeBlanc
  • Chan Kong-sang (Hymanie Chan's real name)
  • P!nk
  • Love Symbol #2
  • 让-吕克·皮卡德
  • 卡米拉·帕克-鲍尔斯
    • 圣女贞德
  • 马特·勒布朗
  • 陈江生(成龙真名)
  • p!nk
  • Love Symbol #2

The 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.

最简单的方法是让用户输入他们的姓名并按原样接受。如果您想将其分解为个人姓名和姓氏以进行个性化,那么我建议您将输入字段分解为两个(或更多)部分,或者简单地询问“首选名称”或“昵称”字段.

I 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.

我怀疑您是否会找到一个可以验证所有名称的正则表达式 - 在您开始尝试之前获取大量样本数据(最好是真实世界)。

回答by Noel M

Paulhas a capital P and your regex doesn't allow for capitalization at the start of the second word.

Paul有一个大写 P 并且您的正则表达式不允许在第二个单词的开头使用大写。

回答by polygenelubricants

Try something like this:

尝试这样的事情:

[A-Z][a-z]+( [A-Z][a-z]+)?

The ?is an optional part that matches the last name. This captures the last name (with a preceding space) in group 1. You can use a non-capturing group (?:...)if you don't need this capture.

?是,最后的名称相匹配的可选部分。这将捕获组 1 中的姓氏(前面有空格)。(?:...)如果不需要此捕获,则可以使用非捕获组。

References

参考



Problem with original pattern

原图有问题

Here's the original pattern:

这是原始模式:

[A-Z]([a-z]+|\s[a-z]+)

Expanding the alternation this matches:

扩展这个匹配的交替:

[A-Z][a-z]+

Or:

或者:

[A-Z]\s[a-z]+

This does match John, and J paul, but it clearly doesn't match John Paul.

这确实匹配John, 和J paul,但显然不匹配John Paul