关于如何使用正则表达式验证姓名和姓氏的建议 [英] Advice on how to validate names and surnames using regex
本文介绍了关于如何使用正则表达式验证姓名和姓氏的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为我的 Ruby on Rails 3 应用程序验证 name 和 surname,所以我发布了 这个问题.有人建议我阅读 程序员相信关于名字的谎言 文章,现在我遇到了麻烦.
I want to validate name and surname for my Ruby on Rails 3 application and so I have posted this question. Someone adviced me to read the Falsehoods Programmers Believe About Names article and now I am in trouble.
验证姓名的正确方法是什么?
Regex 是一种方式,但我应该注意什么?你能给我一个概述吗?
Regex is a way, but what should I be careful? Can you give me an overview?
推荐答案
验证名称的正确方法是:
The correct way to validate names is:
- 接受任何和所有可能的 Unicode 字符.
- 拒绝长度为 0 的输入.
- 确保结果中至少有一个不是
\pM
、\pZ
和\pC
类型的字符;即,既不是组合标记、分隔符之一,也不是其他(通常是控制)字符.像[^\pM\pC\pZ]
这样的字符类就足够了.
- Accept any and all possible Unicode characters.
- Reject inputs of length 0.
- Make sure the result has at least one character that is neither of type
\pM
,\pZ
, nor\pC
; i.e., is neither one of the combining marks, separators, nor other (usually control) characters. A character class like[^\pM\pC\pZ]
should suffice.
那里:尽可能简单.不过,您可能需要 Ruby 1.9 才能正确执行此操作.
There: easy as can be. You will probably need Ruby 1.9 to do this properly, though.
这篇关于关于如何使用正则表达式验证姓名和姓氏的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文