-2

I have a requirement where I have to check String(combination of number and letter only) which Must not contain more than 3 successive characters(only character). for example:

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

Please help me to find out the proper regular expression for the this.

HamZa
  • 14,671
  • 11
  • 54
  • 75
Mahendra Athneria
  • 1,203
  • 3
  • 16
  • 32
  • 2
    This `abcr - allowed` and this `pqra - allowed` are contradicting your requirements. And `aaa2aaa3bb` should this match ? – HamZa Jun 20 '13 at 10:56
  • @HamZa - why abcr and pqra contradictong? aaa2aaa3bb is allowed – Mahendra Athneria Jun 20 '13 at 10:58
  • @devnull yes it is to validate the password. – Mahendra Athneria Jun 20 '13 at 10:59
  • Ah I now understand your requirements, by successive characters you mean successive as in the alphabet. As for using this for password validation : **head-desk** – HamZa Jun 20 '13 at 11:01
  • Oh, pause. The entropy in http://xkcd.com/936/ is far higher. Here you have only [a-zA-Z0-9]. Hope that the system in question wouldn't be connected to the internet. – devnull Jun 20 '13 at 11:06
  • 1
    I have no idea but `(?i)abcd|bcde|cdef|defg|efgh|fghi|ghij|hijk|ijkl|jklm|klmn|lmno|mnop|nopq|opqr|pqrs|qrst|rstu|stuv|tuvw|uvwx|vwxy|wxyz` – falsetru Jun 20 '13 at 11:12
  • 1
    This isn't a good idea at all, for example if I want to set "ove **rstu** dy123" as a password, it will fail since I have consecutive letters. Also why won't you allow special characters ? You need to hash the password anyways. I highly suggest you to review your strategy and check on some awesome answers here on SO. – HamZa Jun 20 '13 at 11:15
  • @HamZa - this is the requirement and i have to validate with regex. – Mahendra Athneria Jun 20 '13 at 11:22
  • @falsetru thanks.forget to add one more condition- aaaa - not allowed – Mahendra Athneria Jun 20 '13 at 11:27
  • 1
    `(?i)abcd|bcde|cdef|defg|efgh|fghi|ghij|hijk|ijkl|jklm|klmn|lmno|mnop|nopq|opqr|pqrs|qrst|rstu|stuv|tuvw|uvwx|vwxy|wxyz|(.)\1{3} ` – falsetru Jun 20 '13 at 13:17
  • @falsetru post it as an answer. it's pretty much what the OP needs, with `([a-z])` instead of `(.)` – CSᵠ Jun 20 '13 at 13:33
  • 1
    `(?i)abcd|bcde|cdef|defg|efgh|fghi|ghij|hijk|ijkl|jklm|klmn|lmno|mnop|nopq|opqr|‌​pqrs|qrst|rstu|stuv|tuvw|uvwx|vwxy|wxyz|([a-z])\1{3}` – falsetru Jun 20 '13 at 13:37
  • @falsetru it is not working for pqrs. any idea why? – Mahendra Athneria Jun 20 '13 at 13:45
  • @MahendraAthneria, What language (or tool) do you use to test the regular expression? Show your code if possible. – falsetru Jun 20 '13 at 13:46
  • @falsetru i am using java – Mahendra Athneria Jun 20 '13 at 14:01
  • @falstru, please don't use comments to present possible answers. – Ro Yo Mi Jun 20 '13 at 14:09
  • 1
    @MahendraAthneria, copy & paste the code in my comment insert invisible special character before pqrs. Maybe that was the reason. – falsetru Jun 20 '13 at 14:30

2 Answers2

3

Description

This expression will look for:

  • four or more sequential letters in alphabetical order (eg: abcd, tuvwxyz)
  • four more or sequential letters which are identical (eg: 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})).)*

enter image description here

Java Code Example:

Input text

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

Code

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

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

)
Ro Yo Mi
  • 14,790
  • 5
  • 35
  • 43
2

Code (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))

Result

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
falsetru
  • 357,413
  • 63
  • 732
  • 636