简体   繁体   English

使用RegEx验证Java中的名字和姓氏

[英]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

Explanation: 说明:

  1. The biggest problem you have is ' and ' is different. 你遇到的最大问题是''是不同的。 You can only achieve that character by copy pasting from the text. 您只能通过从文本中复制粘贴来实现该角色。
  2. Use \\- instead of - in [] since it will be mistaken as a range. []使用\\-而不是-因为它会被误认为是一个范围。 For example: [az] 例如: [az]
  3. You can use \\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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM