我有一个要求,我必须检查不能包含超过 3 个连续字符(仅字符)的字符串(仅数字和字母的组合)。例如:
abcd - 不允许
AbCd - 不允许
abc3 - 允许
abcr - 允许
PQRS - 不允许
pqrs - 不允许
pqra - 允许
aaaa - 不允许
qqqq - 不允许
aaab - 允许
qqqw - 允许
1234 - 允许
1111 - 允许
请帮我找出正确的正则表达式。
我有一个要求,我必须检查不能包含超过 3 个连续字符(仅字符)的字符串(仅数字和字母的组合)。例如:
abcd - 不允许
AbCd - 不允许
abc3 - 允许
abcr - 允许
PQRS - 不允许
pqrs - 不允许
pqra - 允许
aaaa - 不允许
qqqq - 不允许
aaab - 允许
qqqw - 允许
1234 - 允许
1111 - 允许
请帮我找出正确的正则表达式。
此表达式将查找:
abcd
、、tuvwxyz
)aaaa
, qqqqfdadas
)^(?:(?!(?:abcd|bcde|cdef|defg|efgh|fghi|ghij|hijk|ijkl|jklm|klmn|lmno|mnop|nopq|opqr|pqrs|qrst|rstu|stuv|tuvw|uvwx|vwxy|wxyz|(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z)\1{3})).)*
输入文本
abcd - not allowed
AbCd - not allowed
abc3 - allowed
abcr - allowed
PQRS - not allowed
pqrs - not allowed
pqra - allowed
aaaa - not allowed
qqqq - not allowed
aaab - allowed
qqqw - allowed
1234 - allowed
1111 - allowed
代码
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class Module1{
public static void main(String[] asd){
String sourcestring = "source string to match with pattern";
Pattern re = Pattern.compile("^(?:(?!(?:abcd|bcde|cdef|defg|efgh|fghi|ghij|hijk|ijkl|jklm|klmn|lmno|mnop|nopq|opqr|pqrs|qrst|rstu|stuv|tuvw|uvwx|vwxy|wxyz|(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z)\\1{3})).)*",Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
Matcher m = re.matcher(sourcestring);
int mIdx = 0;
while (m.find()){
for( int groupIdx = 0; groupIdx < m.groupCount()+1; groupIdx++ ){
System.out.println( "[" + mIdx + "][" + groupIdx + "] = " + m.group(groupIdx));
}
mIdx++;
}
}
}
火柴
$matches Array:
(
[0] => Array
(
[0] =>
[1] =>
[2] => abc3 - allowed
[3] => abcr - allowed
[4] =>
[5] =>
[6] => pqra - allowed
[7] =>
[8] =>
[9] => aaab - allowed
[10] => qqqw - allowed
[11] => 1234 - allowed
[12] => 1111 - allowed
)
[1] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
)
)
import re
pattern_for_invalid_string = re.compile(
r'(?i)' +
r'^' +
r'abcd|bcde|cdef|defg|efgh|fghi|ghij|hijk|ijkl|jklm|klmn|lmno|' +
r'mnop|nopq|opqr|pqrs|qrst|rstu|stuv|tuvw|uvwx|vwxy|wxyz|' +
r'([a-z])\1{3}' +
r'$')
def check(s):
return pattern_for_invalid_string.search(s) == None
for s in 'abcd AbCd abc3 abcr PQRS pqrs pqra aaaa qqqq aaab qqqw 1234 1111'.split():
allowed = 'allowed' if check(s) else 'not allowed'
print('{} - {}'.format(s, allowed))
abcd - not allowed
AbCd - not allowed
abc3 - allowed
abcr - allowed
PQRS - not allowed
pqrs - not allowed
pqra - allowed
aaaa - not allowed
qqqq - not allowed
aaab - allowed
qqqw - allowed
1234 - allowed
1111 - allowed