简体   繁体   中英

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?

You can use a Negative Lookahead to do this.

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

Live Demo

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");

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM