C#中包含内部空间的姓氏的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/632322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regular Expression in C# for Last Name that includes internal space
提问by Caveatrob
I'd like a Regular Expression for C# that matches "Johnson", "Del Sol", or "Del La Range"; in other words, it should match words with spaces in the middle but no space at the start or at the end.
我想要一个匹配“Johnson”、“Del Sol”或“Del La Range”的 C# 正则表达式;换句话说,它应该匹配中间有空格但开头或结尾没有空格的单词。
回答by Paul Roub
The ? qualifier is your friend. Makes a shortest-possible match instead of a greedy one. Use it for the first name, as in:
这 ?预选赛是你的朋友。进行最短可能匹配而不是贪婪匹配。将其用作名字,如下所示:
^(.+?) (.+)$
Group 1 grabs everything up to the first space, group 2 gets the rest.
第 1 组抢占第一个空间的所有内容,第 2 组获得其余部分。
Of course, now what do you do if the firstname contains spaces?
当然,现在你会怎么做,如果第一名称包含空格?
回答by Andrew Hare
Try something like this:
尝试这样的事情:
^[^\s][\w\s]*[^\s]$
回答by Noldorin
This should do the job:
这应该可以完成这项工作:
^[a-zA-Z][a-zA-Z ]*[a-zA-Z]$
Edit:Here's a slight improvement that allows one-latter names and hyphens/apostrophes in the name:
编辑:这是一个轻微的改进,允许名称中的后一个名称和连字符/撇号:
^[a-zA-Z'][a-zA-Z'- ]*[a-zA-Z']?$
回答by Daniel LeCheminant
^\p{L}+(\s+\p{L}+)*$
This regex has the following features:
此正则表达式具有以下功能:
- Will match a one letter last name (e.g. Malcolm X's last name)
- Will not match last names containing numbers (like anything with a
\w
or a[^ ]
will) - Matches unicode letters
- 将匹配一个字母的姓氏(例如 Malcolm X 的姓氏)
- 不会匹配包含数字的姓氏(就像任何带有 a
\w
或 a[^ ]
will 的名字) - 匹配 Unicode 字母
But what about last names like "O'Connor" or hyphenated last names ... hmm ...
但是像“O'Connor”这样的姓氏或带连字符的姓氏呢……嗯……
回答by dexedrine
I think this is more what you were looking for:
我认为这更符合您的要求:
^[^ ][a-zA-Z ]+[^ ]$
This should match the beginning of the line with no space, alpha characters or a space, and no space at the end.
这应该匹配行的开头,没有空格、字母字符或空格,结尾也没有空格。
This works in irb, but last time I worked with C#, I've used similar regexes:
这在 irb 中有效,但上次我使用 C# 时,我使用了类似的正则表达式:
(zero is good, nil means failed)
(零是好的,nil 表示失败)
>> "Di Giorno" =~ /^[^ ][a-zA-Z ]+[^ ]$/
=> 0
>> "DiGiorno" =~ /^[^ ][a-zA-Z ]+[^ ]$/
=> 0
>> " DiGiorno" =~ /^[^ ][a-zA-Z ]+[^ ]$/
=> nil
>> "DiGiorno " =~ /^[^ ][a-zA-Z ]+[^ ]$/
=> nil
>> "Di Gior no" =~ /^[^ ][a-zA-Z ]+[^ ]$/
=> 0
回答by John Saunders
In the name "?alā? ad-Dīn Yūsuf ibn Ayyūb" (see http://en.wikipedia.org/wiki/Saladdin), which is the first name, and which is the last? What about in the name "Roberto Garcia y Vega" (invented)? "Chiang Kai-shek" (see http://en.wikipedia.org/wiki/Chang_Kai-shek)?
在名称“?alā? ad-Dīn Yūsuf ibn Ayyūb”(参见http://en.wikipedia.org/wiki/Saladdin)中,哪个是名字,哪个是最后一个?“Roberto Garcia y Vega”(发明)这个名字怎么样?“蒋介石”(见http://en.wikipedia.org/wiki/Chang_Kai-shek)?
Spaces in names are the least of your problems! See Personal names in a global application: What to store.
名称中的空格是您的问题中最少的!请参阅全局应用程序中的个人姓名:要存储的内容。
回答by Jason
Here's a better one:
这是一个更好的:
/^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/
Allows standard punctuation and spaces, but cannot start with punctuation.
允许标准标点符号和空格,但不能以标点符号开头。