Java - 全名的正则表达式 [英] Java - Regex for Full Name

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

问题描述

如何验证正则表达式的全名?我只想要字母表(没有数字),只需要正则表达式的空格。这是我到目前为止所做的。你能帮我修一下这个正则表达式吗?非常感谢

How can I validate regex for full name? I only want alphabets (no numericals) and only spaces for the regex. This is what I have done so far. Would you please help me fix the regex? Thank you very much

public static boolean isFullname(String str) {
    boolean isValid = false;
    String expression = "^[a-zA-Z][ ]*$"; //I know this one is wrong for sure >,<
    CharSequence inputStr = str;
    Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);
    if (matcher.matches()) {
        isValid = true;
    }
    return isValid;
}


推荐答案

识别名称的问题是非常以文化为中心,它真的没有可靠的工作希望。我真的建议不要这样做,因为在我所知道的地球上任何国家的任何国家都有 NO 规范形式构成一个人的名字。我可以合法地将我的名字改为#& *∫ΩΔSmith,这不符合任何人的算法。并不是说这个具体的例子是很多人所做的事情,但很多人在考虑数据输入时并没有考虑在ASCII表之外,这会导致问题。

This problem of identifying names is very culture-centric, and it really has no hope of working reliably. I would really recommend against this, as there is NO canonical form for what makes up a person's name in any country, anywhere on Earth that I know of. I could legally change my name to #&*∫Ω∆ Smith, and that's not going to fit into anyone's algorithm. It's not that this specific example is something that a lot of people do, but many people don't think outside of the ASCII table when considering data input, and that's going to lead to problems.

您可以反对这种情况发生的可能性,但在全球化的世界中,您的所有用户都越来越不可能为他们的名字配备英语拼音拼写。您也很可能拥有来自不具有名/姓概念的文化的用户。不要认为,即使您的应用程序仅在特定国家/地区运行,您的某些用户也不会来自其他地方(人们会一直从一个国家/地区迁移到另一个国家/地区,而其中一些用户可能希望使用您的软件)。

You can argue against the probability of this happening, but in a global world, it's increasingly unlikely that ALL of your users are going to have English-transliterated spellings for their names. It's also very possible that you'll have users from cultures that don't have a concept of first/last name. Don't assume that, even if your application is only running in a given country, that some of your users won't be from other places (people move from country to country all the time, and some of them might want to use your software).

保护你的应用程序免受SQL注入这样的字段(如果你将它们存储在数据库中),并保留它。

Protect your app against SQL injection for fields such as this (if you're storing these in a DB), and leave it at that.

这篇关于Java - 全名的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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