0

I have following individual regular expressions I want to combine them using AND condition , I am using them for validating password

.[A-Z]+. - validate uppercase (one letter uppercase min)

.[0-9]+. - validate number ( one number atleast )

.[a-z]+. - validate lowercase ( one lower case minimum )

.{8,} - validate min character 8

.[^A-Za-z0-9]. - validate special character (atleast one special character )

(.)\1 - validate consecutive characters (no consecutive characters )

Right now I am validating every character separately , but i want to do it in one function only

I tried following way of combining

/^((.)\1)(.[A-Z]+.)(.[a-z]+.)(.[0-9]+.)(.[^A-Za-z0-9].).*$/

Above doesn't have all the expressions but I am trying to show how I have done.

Emma
  • 27,428
  • 11
  • 44
  • 69
vishal dharankar
  • 7,536
  • 7
  • 57
  • 93
  • 2
    It would be really helpful if you explain, in English, what rules your string must match. Show clear examples of good strings and show clear examples of bad strings and show why they are bad. – rmaddy May 21 '19 at 16:38
  • 1
    Better to do it separately so you can provide feedback to the user about the password requirements – Leo Dabus May 21 '19 at 16:45
  • @rmaddy my bad , added explanation – vishal dharankar May 21 '19 at 16:50
  • @LeoDabus actually it’s resulting into a poor code with nested if else , and I don’t want to show any specific message as such – vishal dharankar May 21 '19 at 16:51
  • @vishaldharankar https://stackoverflow.com/questions/31954416/regex-for-alphanumeric-without-special-characters-swift-ios/31954533#31954533. Btw you can use the new character properties to do your string validation no need to use a regex – Leo Dabus May 21 '19 at 16:51
  • That's not what I meant at all. I understand what each individual regex means. You need to provide an overall description of the requirements you are trying to match. And show examples. – rmaddy May 21 '19 at 16:52

1 Answers1

1

One option is to use a set of positive lookaheads using negated character classes:

^(?=[^A-Z]*[A-Z])(?=[^a-z]*[a-z])(?=[^0-9]*[0-9])(?!.*(.)\1)(?=[a-zA-Z0-9]*[^A-Za-z0-9\s])\S{8,}$

That will match:

  • ^ Start of string
  • (?=[^A-Z]*[A-Z]) Assert uppercase
  • (?=[^a-z]*[a-z]) Assert lowercase
  • (?=[^0-9]*[0-9]) Assert digit
  • (?!.*(.)\1) Assert no consecutive char
  • (?=[a-zA-Z0-9]*[^A-Za-z0-9\s]) Assert char other than listed including a whitespace char (assuming that would not be allowed)
  • \S{8,} Match 8+ times a non whitespace char
  • $ End of string

Regex demo

Note that \S for the allowed chars is a broad match, you could specify what you would allow to match using a character class.

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • Thanks would give it a try now – vishal dharankar May 21 '19 at 16:52
  • doesnt work tried "Vishal123@" it says doesnt match . – vishal dharankar May 21 '19 at 18:09
  • Here is the code -------- func validate() -> Bool { // let capitalLetterRegEx = "/^((.)\\1)(.*[A-Z]+.*)(.*[a-z]+.*)(.*[0-9]+.*)(.*[^A-Za-z0-9].*).*$/" let capitalLetterRegEx = "^(?=[^A-Z]*[A-Z])(?=[^a-z]*[a-z])(?=[^0-9]*[0-9])(?=.*(.)\\1)(?=[a-zA-Z0-9]*[^A-Za-z0-9\\s])\\S{8,}$" let texttest = NSPredicate(format:"SELF MATCHES %@", capitalLetterRegEx) guard texttest.evaluate(with: "Vishal456!") else { return false } return true } – vishal dharankar May 21 '19 at 18:09
  • @vishaldharankar that is because `Vishal456!` does not contain a consecutive character See https://regex101.com/r/SZmCWe/2 – The fourth bird May 21 '19 at 18:23
  • actually may be my explanation was misleading what I mean was no consecutive characters – vishal dharankar May 21 '19 at 18:25
  • Actually my explanation is correct , I have mentioned it clearly – vishal dharankar May 21 '19 at 18:25
  • @vishaldharankar You are right, I am sorry..my bad. Then you could use a negative lookahead instead. I have updated my answer as well. https://regex101.com/r/DGHfVv/1 – The fourth bird May 21 '19 at 18:27