manohar rao
manohar rao

Reputation: 13

Regular Expression for no repeat character

I have the below regular expression.

^((?=.{10,32}$)(?=.*[A-Z])(?=.*[a-z]))

The regular expression has the following functionality:

  1. Passwords will contain at least (1) upper case letter
  2. Passwords will contain at least (1) lower case letter
  3. Length of password to be between 10 to 32

I want to add one more validation. I do not want any continuous repeating character.

Can you please help me?

Upvotes: 1

Views: 1659

Answers (2)

hwnd
hwnd

Reputation: 70732

You can use a Negative Lookahead to do this.

^(?=.{10,32}$)(?=.*[A-Z])(?=.*[a-z])(?!.*(.)\1).+$

Live Demo

Upvotes: 2

mybirthname
mybirthname

Reputation: 18127

This would match any pair of identical characters:

"(.)\1"

Here little program.

    static void Main(string[] args)
    {
        string a = "12223";
        string b = "P12345";


        bool z = Regex.IsMatch(a,@"(.)\1");
        bool x = Regex.IsMatch(b,@"(.)\1");

    }

Upvotes: 1

Related Questions