正则表达式 - 名称验证

发布于 12-22 07:17 字数 924 浏览 6 评论 0原文

我正在寻找一个正则表达式来检查名称。我搜索了网络,并使用了 StackOverflow 在发布这个问题时给我的建议。

我也知道这是可能的,但我正在寻找一个 regex-1-liner 来保持我的代码干净、简单,最重要的是:快速。

我需要什么: 一个正则表达式,用于在人们注册到我的网站时检查他们的姓名。我想允许名称为:

  • Name
  • Name Surename
  • Name O'brian
  • Name Surename secondarysurname
  • Name Surename-surnametwo
  • N. Surename

但我不想允许名称为:

  • Name(双空格键)
  • Name -- Surename(双减号)
  • Name- -'(只是废话)

好吧,我想你明白我的意思以及我不想允许的事情。

我只想使用 a-zA-Z 和 - 。 ' 我想这是我唯一需要允许的事情。这 - 。 ' 符号只能在单词之间或之后使用一次。因为像“名字 O”“Brian”这样的名字不存在。

但应该允许使用“姓名、第二姓氏、第三姓氏”之类的名称。所以一个空格键和一个减号。

我使用 http://public.kvalley.com/regex/regex.asp 想出了几个正则表达式' 和其他正则表达式程序。但我只是正则表达式的菜鸟。

我希望有人对正则表达式了解很多并且愿意帮助我。因为此刻..我被困住了:(

提前感谢,

Jelmer

ps。如果您对我的问题有任何疑问。请询问他们,因为我真的很想得到您的帮助!

I am looking for a regular expression to check names. I have searched the net and also used the suggestion that were given me by StackOverflow while posting this question.

I also know it's possible in stages, but I'm looking for a regex-1-liner to keep my code clean, simple and most important: fast.

What do I need:
A regular expression that checks names of people while they are registering to my site. I want to allow names as:

  • Name
  • Name surename
  • Name O'brian
  • Name surename secondarysurname
  • Name surename-surnametwo
  • N. Surename

But I don't want to allow names as:

  • Name (double spacebar)
  • Name -- surename (double minus)
  • Name--' (just bullshit)

Well, I think you understand what I mean and what I don't want to allow.

I only want to use a-zA-Z and - . '
I think that's the only thing I need to allow. The - . ' signs can only be used once between or after a word. Since a name like 'name O''Brian' does not exist.

But a name like 'Name surename secondary-thirdsurname' should be allowed. So one spacebar and one minus sign.

I came up with several regex' using http://public.kvalley.com/regex/regex.asp and other regex programs. But I'm just a noob with regex'.

I hope somebody knows a lot about regular expressions and is willing to help me. Because at the moment.. I'm stuck :(

Thanks in advance,

Jelmer

ps. If you have any questions regarding my question. Please ask them because I'd really like to have your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哆啦不做梦2024-12-29 07:17:34

适用于编码许多方面(尤其是正则表达式设计)的一般经验法则是,您的代码可以是:

  1. 简单
  2. 干净
  3. 快速

选择两个。

除此之外,我保证一个 -这个复杂的衬里永远不会干净。将其分解为正则表达式变量并对其进行自由注释。稍后,你会感谢你所做的!当您使用它时,将其转换为可以重用的通用名称验证器类。虽然 Perl 可能被专家们搞得完全不神圣,但当我们遵循与其他更结构化的语言相同的礼貌和清洁法则时,它的美丽往往就会显现出来。

TL;DR:不要让它成为一句台词。请。

A general rule of thumb that applies to many aspects of coding, but especially to regex design, is, your code can be:

  1. Simple
  2. Clean
  3. Fast

Pick two.

In addition to that, I guarantee that a one-liner this complex will never be clean. Break it up into regex variables and comment it liberally. Later on, you'll be thankful that you did! While you're at it, turn it into a generic name validator class that you can reuse. While Perl can be quite munged by experts into something totally unholy, it's beauty often comes out when we follow the same laws of politeness and cleanliness that we follow in other, more structured languages.

TL;DR: Don't make it a one-liner. Please.

很糊涂小朋友2024-12-29 07:17:34

这不是万无一失的,但应该给你一个正确方向的提示

([a-zA-ZáéíóúñÑ]+ ?'?-?)+

更新:根据@Tim的建议,这是一个更好的方法

([a-zA-ZáíóúñÑö]+( |'|-)?)+

this is not bulletproof but should give you a hint in the right direction

([a-zA-ZáéíóúñÑ]+ ?'?-?)+

UPDATE: Heres a better approach according to @Tim s suggestion

([a-zA-ZáíóúñÑö]+( |'|-)?)+
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文