正则表达式的名称 [英] Regex for names

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

问题描述

刚刚开始探索正则表达式的奇迹".作为一个从试验和错误中学习的人,我真的很努力,因为我的试验抛出了不成比例的错误……我的实验是在PHP中使用ereg().

Just starting to explore the 'wonders' of regex. Being someone who learns from trial and error, I'm really struggling because my trials are throwing up a disproportionate amount of errors... My experiments are in PHP using ereg().

无论如何.我分别使用名字和姓氏,但现在使用相同的正则表达式.到目前为止,我有:

Anyway. I work with first and last names separately but for now using the same regex. So far I have:

^[A-Z][a-zA-Z]+$  

任何以大写字母开头的长度字符串,其余仅包含字母(大写或不大写).但是,我分崩离析的地方是处理几乎在任何地方都可能发生的特殊情况.

Any length string that starts with a capital and has only letters (capital or not) for the rest. But where I fall apart is dealing with the special situations that can pretty much occur anywhere.

  • 带连字符的名字(Worthington-Smythe)
  • 带有使徒身份的名字(D'Angelo)
  • 带空格的名字(范德汉普顿)-在此阶段可能不需要或不需要中间的大写字母.
  • 联名(Ben& Jerry)

也许还有其他名称可能是我没有想到的,但是我怀疑如果我能解决这个问题,我可以添加一下.我敢肯定,在某些情况下,一种或多种情况会以一种名字出现.

Maybe there's some other way a name can be that I'm no thinking of, but I suspect if I can get my head around this, I can add to it. I'm pretty sure there will be instances where more than one of these situations comes up in one name.

因此,我认为最重要的是让我的正则表达式也接受空格,连字符,&符和撇号-但在名称的开头或结尾在技术上是正确的.

So, I think the bottom line is to have my regex also accept a space, hyphens, ampersands and apostrophes - but not at the start or end of the name to be technically correct.

推荐答案

  • 带连字符的名字(Worthington-Smythe)
  • 在第二个字符类中添加-.最简单的方法是在开始时添加它,这样就不可能将其解释为范围修饰符(如a-z所示).

    Add a - into the second character class. The easiest way to do that is to add it at the start so that it can't possibly be interpreted as a range modifier (as in a-z).

    ^[A-Z][-a-zA-Z]+$

    • 带有使徒身份的名字(D'Angelo)
    • 一种简单的方法是如上所述,给出:

      A naive way of doing this would be as above, giving:

      ^[A-Z][-'a-zA-Z]+$

      别忘了您可能需要将其转义到字符串中!给定您的示例,更好"的方式可能是:

      Don't forget you may need to escape it inside the string! A 'better' way, given your example might be:

      ^[A-Z]'?[-a-zA-Z]+$

      第二个位置将允许单引号.

      Which will allow a possible single apostrophe in the second position.

      • 带空格的名字(范德汉普顿)-在此阶段可能不需要或不需要中间的大写字母.

      在这里,我很想再做一次我们的天真做法:

      Here I'd be tempted to just do our naive way again:

      ^[A-Z]'?[- a-zA-Z]+$

      一种可能更好的方法是:

      A potentially better way might be:

      ^[A-Z]'?[- a-zA-Z]( [a-zA-Z])*$

      在结尾处查找多余的单词.如果您要在大量额外的文本中匹配名称,那么这可能不是一个好主意,但是同样,原件也不会做得很好.

      Which looks for extra words at the end. This probably isn't a good idea if you're trying to match names in a body of extra text, but then again, the original wouldn't have done that well either.

      • 联名(Ben& Jerry)

      这时您不再查看单个名称了吗?

      At this point you're not looking at single names anymore?

      无论如何,如您所见,正则表达式具有快速增长的习惯...

      Anyway, as you can see, regexes have a habit of growing very quickly...

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

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