Regular Expression to match 3 or more Consecutive Sequential Characters and Consecutive Identical Characters

前端 未结 15 2126
北荒
北荒 2020-11-28 11:41

I need regular expressions to match the below cases.

  1. 3 or more consecutive sequential characters/numbers; e.g. 123, abc, 789, pqr, etc.
  2. 3 or more cons
相关标签:
15条回答
  • 2020-11-28 12:14

    Thanks All for helping me.

    For the first case - 3 or more consecutive sequential characters/numbers; e.g. 123, abc, 789, pqr, etc. I used below code logic. Pls share your comments on this.

    public static boolean validateConsecutiveSeq(String epin) {
        char epinCharArray[] = epin.toCharArray();
        int asciiCode = 0;
        boolean isConSeq = false;
        int previousAsciiCode = 0;
        int numSeqcount = 0;
    
        for (int i = 0; i < epinCharArray.length; i++) {
            asciiCode = epinCharArray[i];
            if ((previousAsciiCode + 1) == asciiCode) {
                numSeqcount++;
                if (numSeqcount >= 2) {
                    isConSeq = true;
                    break;
                }
            } else {
                numSeqcount = 0;
            }
            previousAsciiCode = asciiCode;
        }
        return isConSeq;
    }
    
    0 讨论(0)
  • 2020-11-28 12:15

    3 or more consecutive sequential characters/numbers ex - 123, abc, 789, pqr etc.

    Not possible with regular expressions.

    3 or more consecutive identical characters/numbers ex - 111, aaa, bbb. 222 etc.

    Use a pattern of (?i)(?:([a-z0-9])\\1{2,})*.

    If you want to check the whole string, use Matcher.matches(). To find matches within a string, use Matcher.find().

    Here's some sample code:

    final String ps = "(?i)(?:([a-z0-9])\\1{2,})*";
    final String psLong =
            "(?i)\t\t\t# Case insensitive flag\n"
                    + "(?:\t\t\t\t# Begin non-capturing group\n"
                    + " (\t\t\t\t# Begin capturing group\n"
                    + "  [a-z0-9]\t\t# Match an alpha or digit character\n"
                    + " )\t\t\t\t# End capturing group\n"
                    + " \\1\t\t\t\t# Back-reference first capturing group\n"
                    + " {2,}\t\t\t# Match previous atom 2 or more times\n"
                    + ")\t\t\t\t# End non-capturing group\n"
                    + "*\t\t\t\t# Match previous atom zero or more characters\n";
    System.out.println("***** PATTERN *****\n" + ps + "\n" + psLong
            + "\n");
    final Pattern p = Pattern.compile(ps);
    for (final String s : new String[] {"aa", "11", "aaa", "111",
            "aaaaaaaaa", "111111111", "aaa111bbb222ccc333",
            "aaaaaa111111bbb222"})
    {
        final Matcher m = p.matcher(s);
        if (m.matches()) {
            System.out.println("Success: " + s);
        } else {
            System.out.println("Fail: " + s);
        }
    }
    

    And the output is:

    ***** PATTERN *****
    (?i)(?:([a-z0-9])\1{2,})*
    (?i)            # Case insensitive flag
    (?:             # Begin non-capturing group
     (              # Begin capturing group
      [a-z0-9]      # Match an alpha or digit character
     )              # End capturing group
     \1             # Back-reference first capturing group
     {2,}           # Match previous atom 2 or more times
    )               # End non-capturing group
    *               # Match previous atom zero or more characters
    
    
    Fail: aa
    Fail: 11
    Success: aaa
    Success: 111
    Success: aaaaaaaaa
    Success: 111111111
    Success: aaa111bbb222ccc333
    Success: aaaaaa111111bbb222
    
    0 讨论(0)
  • 2020-11-28 12:17

    Try this for the first question.

    returns true if it finds 3 consecutive numbers or alphabets in the arg

    function check(val){
        for (i = 0; i <= val.length - 3; i++) {
          var s1 = val.charCodeAt(i);
          var s2 = val.charCodeAt(i + 1);
          var s3 = val.charCodeAt(i + 2);
          if (Math.abs(s1 - s2) === 1 && s1 - s2 === s2 - s3) {
            return true;
          }
        }
    
        return false;
    }
    
    console.log(check('Sh1ak@ki1r@100'));
    
    0 讨论(0)
  • 2020-11-28 12:22

    To my knowledge, the first case is indeed not possible. The regex engine doesn't know anything about the order of the natural numbers or the alphabet. But it's at least possible to differentiate between 3 or more numbers and 3 or more letters, for example:

    [a-z]{3,}|[A-Z]{3,}|\d{3,}
    

    This matches abcd, ABCDE or 123 but doesn't match ab2d, A5c4 or 12z, for example. According to this, the second case can be correctly given in a shorter version as:

      (\w)\1{2,}
    
    0 讨论(0)
  • 2020-11-28 12:22
    • 3 or more consecutive sequential characters/numbers; e.g. 123, abc, 789, pqr, etc.

      (?:(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){2,}\d|(?:a(?=b)|b(?=c)|c(?=d)|d(?=e)|e(?=f)|f(?=g)|g(?=h)|h(?=i)|i(?=j)|j(?=k)|k(?=l)|l(?=m)|m(?=n)|n(?=o)|o(?=p)|p(?=q)|q(?=r)|r(?=s)|s(?=t)|t(?=u)|u(?=v)|v(?=w)|w(?=x)|x(?=y)|y(?=z)){2,}[\p{Alpha}])
      

      https://regex101.com/r/5IragF/1

    • 3 or more consecutive identical characters/numbers; e.g. 111, aaa, bbb, 222, etc.

      ([\p{Alnum}])\1{2,}
      

      https://regex101.com/r/VEHoI9/1

    0 讨论(0)
  • 2020-11-28 12:23

    All put together:

    ([a-zA-Z0-9])\1\1+|(abc|bcd|cde|def|efg|fgh|ghi|hij|ijk|jkl|klm|lmn|mno|nop|opq|pqr|qrs|rst|stu|tuv|uvw|vwx|wxy|xyz|012|123|234|345|456|567|678|789)+

    3 or more consecutive sequential characters/numbers; e.g. 123, abc, 789, pqr, etc.

    (abc|bcd|cde|def|efg|fgh|ghi|hij|ijk|jkl|klm|lmn|mno|nop|opq|pqr|qrs|rst|stu|tuv|uvw|vwx|wxy|xyz|012|123|234|345|456|567|678|789)+

    3 or more consecutive identical characters/numbers; e.g. 111, aaa, bbb, 222, etc.

    ([a-zA-Z0-9])\1\1+

    https://regexr.com/4727n

    0 讨论(0)
提交回复
热议问题