正则表达式超过三个连续字符

时间:2013-06-20 10:49:15

标签: java regex regular-language

我有一个要求,我必须检查字符串(仅限数字和字母的组合) 其中不得包含超过3个连续字符(仅限字符)。例如:

  

abcd - 不允许

     

AbCd - 不允许

     

abc3 - 允许

     

abcr - 允许

     

PQRS - 不允许

     

pqrs - 不允许

     

pqra - 允许

     

aaaa - 不允许

     

qqqq - 不允许

     

aaab - 允许

     

qqqw - 允许

     

1234 - 允许

     

1111 - 允许

请帮我找出正确的正则表达式。

2 个答案:

答案 0 :(得分:3)

描述

此表达式将查找:

  • 按字母顺序排列的四个或更多连续字母(例如:abcdtuvwxyz
  • 四个相同或相同的连续字母(例如:aaaaqqqqfdadas

^(?:(?!(?: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})).)*

enter image description here

Java代码示例:

输入文字

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] => 
        )

)

答案 1 :(得分:2)

代码(Python)

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