2

These are the requirements but I guess it's too complicated for my regular expression skills...

. between 6 and 10 alphanum characters
. allowed A-Z,a-z,0-9,@,$,_
. Must begin with a letter
. Must contain at least one number
. cannot contain two consecutive identical characters
. cannot contain two consecutive identical numbers

I know the basic of regular expression such as [A-Za-Z] = characters only etc... but when it comes to consecutive character and stuff...

8
  • 3
    Why such strange restrictions on a password? Why not allow all characters and arbitrary length? Commented May 29, 2012 at 11:24
  • 5
    I'll be much easier to do this with several regular expressions than all in one, I hope that's what you're going for. Commented May 29, 2012 at 11:24
  • 1
    Multiple regular expressions would also allow for meaningful feedback to the user. Commented May 29, 2012 at 11:26
  • 1
    Why are you making people's passwords to adhere to a certain format? Not only is this so very annoying for the user, but it reduces security since now an attacker knows every password in the system must be within those very limiting restrictions. Seriously, 10 characters maximum? You must be joking. This is bad practice and you should feel bad. Commented May 29, 2012 at 11:36
  • 1
    (If this is for an assignment then the school/uni should feel bad for putting the idea in people's heads that password format/size restrictions are OK.) Commented May 29, 2012 at 11:43

3 Answers 3

1

Try this

((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W]).{6,20})

Description of above Regular Expression:

(           # Start of group
  (?=.*\d)      #   must contains one digit from 0-9
  (?=.*[a-z])       #   must contains one lowercase characters
  (?=.*[A-Z])       #   must contains one uppercase characters
  (?=.*[\W])        #   must contains at least one special character
              .     #     match anything with previous condition checking
                {6,20}  #        length at least 6 characters and maximum of 20 
)           # End of group

"/W" will increase the range of characters that can be used for password and pit can be more safe.

Sign up to request clarification or add additional context in comments.

Comments

0
        string pattern1 = @"^[a-zA-Z]([a-zA-Z])*"; //start and any number of characters
        string pattern2 = @"[0-9]+"; //one number or more numbers
        string pattern3 = @"[@#$%]*"; // special symbol allowed
        string pattern4 = @"(.)\1";//consecutive characters
        string pattern5 = @"^(.){6,10}$"; //min max

Comments

0

should you want to validate the password you can use groups to do soo ;

(?<a>[a-zA-Z])?(?<b>[0-9])?(?<c>[@%$#/\\\(\)])?

Will give you a match in any of the 3 groups (a,b and c)

uper and lower characters will be in group a

numeric characters will be in group b

and special characters will be in group c

you can use the regex.match.groups("a").count to see if any characters from group a could be found

if you find characters in all 3 groups, the password is strong.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.