0

I am trying to validate a password field using regex in Swift/iOS the feature of the passwords are:

  • 1 uppercase at least
  • 1 number at least

(other than that lowercase and symbols are allowed of course, but those are the required)

this is my current RegEx format:

let passwordRegex = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[*.!@$%^&(){}[]:;<>,.?/~_+-=|\\])$"

this is my password: Test@123

When I run the simulator it errors with the following:

Thread 1: "Can't do regex matching, reason: Can't open pattern U_REGEX_MISSING_CLOSE_BRACKET (string Test@123, pattern ^(?=.[0-9])(?=.[a-z])(?=.[A-Z])(?=.[*.!@$%^&(){}[]:;<>,.?/~_+-=|\])$, case 0, canon 0)"

What is wrong whith my RegEx string?

Thorvald
  • 3,424
  • 6
  • 40
  • 66
  • it has `/` and i dont know if thats the issue... – Ghost Ops Sep 27 '21 at 11:55
  • You are missing a backslash at the end of your regex before the forward slash: ```^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[*.!@$%^&(){}[]:;<>,.?\/~_+-=|\\])$``` – SubChord Sep 27 '21 at 11:56
  • @SubChord I tried removing the symbols all at once but it didn't work as well (the symbols are not required for the field they are optional) – Thorvald Sep 27 '21 at 11:58
  • 2
    Escape the square brackets in the character class (`U_REGEX_MISSING_CLOSE_BRACKET` signal exactly that). Besides, once you fix that, you should also make sure you consume the whole string, keep the hyphen at the end of the character class, and use principle of contrast (and raw string literal): ``let passwordRegex = #"^(?=\D*\d)(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=[^*.!@$%^&(){}\[\]:;<>,.?/~_+=|\\-]*[*.!@$%^&(){}\[\]:;<>,.?/~_+=|\\-]).*"#`` – Wiktor Stribiżew Sep 27 '21 at 11:59

0 Answers0