问题描述
在以下情况下我想知道正则表达式:
该字符串应仅包含字母。它必须以大写字母开头,然后是小写字母。然后可以是小写字母或大写字母。
^ [AZ] [az] [A-Za-z] * $
但是字符串也不能包含任何连续的大写字母。
也就是说, HttpHandler
是正确的,但是 HTTPHandler
是错误的。
编辑:2015-10-26:感谢您的支持-但请查看,特别是如果您是为网络开发或更国际化的东西。
Oren Trutners的答案不太正确(请参阅示例输入
这里是正确的解决方案:
(?!^。* [AZ] {2,}。* $)^ [A-Za-z] * $
编辑:
(?!^。* [AZ] { 2,}。* $)//如果有两个或多个连续的大写字母
^ [A-Za-z] * $ //不匹配整个表达式
/ edit
解决方案的关键是否定的前瞻,请参见:
I want to know the regexp for the following case:
The string should contain only alphabetic letters. It must start with a capital letter followed by small letter. Then it can be small letters or capital letters.
^[A-Z][a-z][A-Za-z]*$
But the string must also not contain any consecutive capital letters. How do I add that logic to the regexp?
That is, HttpHandler
is correct, but HTTPHandler
is wrong.
Edit: 2015-10-26: thanks for the upvotes - but take a look at tchrist's answer, especially if you develop for the web or something more "international".
Oren Trutners answer isn't quite right (see sample input of "RightHerE" which must be matched but isn't)
Here is the correct solution:
(?!^.*[A-Z]{2,}.*$)^[A-Za-z]*$
edit:
(?!^.*[A-Z]{2,}.*$) // don't match the whole expression if there are two or more consecutive uppercase letters
^[A-Za-z]*$ // match uppercase and lowercase letters
/edit
the key for the solution is a negative lookahead see: http://www.regular-expressions.info/lookaround.html
这篇关于用于检查是否在字符串中连续找到大写字母的正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!