2014-09-05 102 views
0

这个给我的任务带来麻烦。正则表达式 - 限制字符串中的字符数量

假设的字母:

-any lowercase or uppercase letter 
-0-9 decimal digits 
-_ 
-$ 
-% 

我想写的是给我的字符串表达式:

-starts with a uppercase letter or one of the three symbols 
-can only have at most 6 lowercase or uppercase letters 

我想尝试像

/^[a-z|_|$|%][a-z|A-Z|_|$|%]* {0,3} 

,但我在跟踪“最多”情况时遇到问题,具体取决于初始角色

编辑:遗忘的例子。

_ababab <- OK 
ab%$aaaa <- OK 
_abababa <- NOT OK, because there is more than 6 alphabet characters 
a$ababab <- NOT OK, because there is more than 6 alphabet characters 
+0

你能解释一下这个'只能有最多6小写字母或大写字母? – 2014-09-05 03:23:22

+0

删除出现在角色类中的'|'符号。 – 2014-09-05 03:24:02

+0

我认为这意味着字符串中不能包含6个以上的小写字母或6个大写字母,包括第一个字母。 – 2014-09-05 03:24:52

回答

0

我想你需要添加类似(?=.*[A-Z]{,6})(?=.*[a-z]{,6})

+0

如果大写和小写字母不连续怎么办? – 2014-09-05 03:29:36

0

我想你想这样的事情,

^[A-Za-z](?:[^A-Za-z]*[A-Za-z][^A-Za-z]*){5}$|^[_$%](?:[^A-Za-z]*[A-Za-z][^A-Za-z]*){6}$ 

DEMO

相关问题