Java 正则表达式验证全名只允许空格和字母 [英] Java Regex to Validate Full Name allow only Spaces and Letters

查看:72
本文介绍了Java 正则表达式验证全名只允许空格和字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望正则表达式仅验证字母和空格.基本上这是为了验证全名.例如:Steve Collins 先生或 Steve Collins 我试过这个正则表达式.[a-zA-Z]+.?"但是没有用.有人可以帮助我吗附言我使用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();

}

推荐答案

关于:

  • 彼得·穆勒
  • 弗朗索瓦·奥朗德
  • 帕特里克·奥布莱恩
  • 西尔瓦娜·科赫-梅林

验证名称是一个难题,因为有效的名称不仅仅由字母 A-Z 组成.

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 正则表达式验证全名只允许空格和字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆