-3

This question has obviously been asked many times regarding how to make a regex using c# to meet the requirements. I require a regex that..

  • Is at least 8 characters long
  • At least 1 upper case character
  • 1 number
  • 1 special character !@#$%^&*()
  • password not the same as the login name(easy to check)
  • not contain more than 2 recurring characters e.g. aaa123

Trying to work this out myself i was thinking to search for how to do each 1 of these requirements one by one and build the regex up as i go. Is this the right approach?

I feel like i could be missing something. I am reading up about regex expressions and how to create a complex one. Your feedback much needed.

Thanks.

Costas Aletrari
  • 387
  • 5
  • 22
  • @Tushar I think OP's problem is in combination of that code with this extra part *not contain more than 2 recurring characters e.g. aaa123* ;). – shA.t Sep 06 '17 at 06:51
  • 2
    [Mandatory CH link](https://blog.codinghorror.com/password-rules-are-bullshit/) – F.P Sep 06 '17 at 06:52

1 Answers1

2

I think you can use a Regex like this:

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()])(?!.*userName)(?!.*(.)\1{2,}).{8,}$

Explanation:

^                         // from start
 (?=.*[a-z])              // has at least one lower case character
 (?=.*[A-Z])              // has at least one upper case character
 (?=.*\d)                 // has at least one digit
 (?=.*[!@#$%^&*()])       // has at least one special character
 (?!.*userName)           // has not userName => set it by a variable
 (?!.*(.)\1{2,})          // has not an repeated character more than twice
 .{8,}                    // has a length of 8 and more
$                         //to the end
shA.t
  • 16,580
  • 5
  • 54
  • 111
  • 2
    https://stackoverflow.com/a/5142164/47672 . Anyway this question is exact duplicate of already existing one. It should be closed. – 0x49D1 Sep 06 '17 at 06:51
  • @0x49D1 Regex are so unreadable - OP *needs not contain more than 2 recurring characters* instead of *Ensure string has three lowercase letters* ;). – shA.t Sep 06 '17 at 06:53
  • This doesn't meet the OP's requirements anyway - 1 number, even if there wasn't a duplicate, the op still hasn't shown any effort on their own part. – Sayse Sep 06 '17 at 06:55