Regex FullName Pattern

筅森魡賤 提交于 2021-01-28 19:51:05

问题


Tried looking around for a regex pattern for a full name and just can't seem to find one.

Ideally would match like

Tom Franklin
tom Franklin
tom franklin
tom franklin-jones

i.e. allow 1 space in the middle and some basic hyphens etc but thats all. Does any one know how to do this ?

Edit: Including

René Hadron van der Ööps

回答1:


You can use /([\p{L}'-]+) ([\p{L}'-]+)/ to catch all possible letter. Or /([a-z'-]+) ([a-z'-]+)/i to catch only ascii letters.

The group ([\p{L}'-]+) means [\p{L}'-] repeated at least one time. And \p{L} means any unicode letter. The - at the end means that "-" is allowed in names.

Note : When you capture characters with [] the hyphen must be either at the start of your characters set or at the end.


Edit :

Mr. O'Brien is happy now.


Resources :

  • regular-expressions.info - unicode
  • regular-expressions.info - character sets



回答2:


\w+\s+[a-zA-Z-]+



来源:https://stackoverflow.com/questions/3702737/regex-fullname-pattern

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!