我需要一个符合以下条件的RegEx:
"United States" // match
"United States" // not match
"united States" // not match
"United states" // not match
" United States" // not match
"United States " // not match
例如,它需要每个单词以大写字母开头,并且单词之间只有一个空格。它还必须拒绝任何尾随或领先的空白字符。
由于
答案 0 :(得分:4)
您的描述非常模糊,但作为一般情况,^[A-Z][a-z]*( [A-Z][a-z]*)*$
应该在没有IgnoreCase标志的情况下工作。
答案 1 :(得分:0)
像这样:?
^[A-Z][a-z]* [A-Z][a-z]*$
答案 2 :(得分:0)
以下类似于@ dotNET的答案,但它考虑了非ASCII字符类:
^[\p{Lu}\p{Lt}]\w*( [\p{Lu}\p{Lt}]\w*)*$
我在这里允许使用中间字非小写字符,因为在unicode中,有更多字符类,而不仅仅是小写和大写。
要匹配严格小写的中间字母,
^[\p{Lu}\p{Lt}]\p{Ll}*( [\p{Lu}\p{Lt}]\p{Ll}*)*$