I am making a sign-in/up function on Swift. I am trying to validate a password using regex. The passwords have the following requirements
- At least 7 characters long
- At least one uppercase letter
- At least one number
This is the validation regex I was using: "^(?=.[A-Z])(?=.[0-9]){7}$"
And this is my code
let passwordTest = NSPredicate(format: "SELF MATCHES %@", "^(?=.[A-Z])(?=.[0-9]){7}$")
Whenever I try to log in with a password that meets these requirements (eg. Greatpass13), Xcode gives me an error saying
Thread 1: "Can't do regex matching, reason: (Can't open pattern U_REGEX_RULE_SYNTAX (string Greatpass13, pattern ^(?=.[A-Z])(?=.[0-9]){7}$, case 0, canon 0))"
3
Answers
you forgot to add dot before counter 7
at least one uppercase,
at least one digit
at least one lowercase
min 7 characters total
Short Explanation
(?=^.{7,}$)
At least 7 characters long(?=^.*[A-Z].*$)
At least one uppercase letter(?=^.*d.*$)
At least one number.*
Match the string that contains all assertionsSee the regex demo
Swift Example
Here is the updated regex based on yours.
Also you need to add {7,} at the end otherwise it will only match for 7 characters and not more than that.
You can test your regex at:
https://regex101.com/