Swift 4.1
I need to validate a password, but my last condition (regex) is who didnt match with the pattern, i got this regex to validate the last condition i need to verify if the password has special characters if has validate if the characters stay in this set (?=.?[(!@$%^()_-+{};:.,)]) if they are out of set i need to know and show alert, my code looks like this:
extension String{
func isValidPassword(minCharacters : Int) -> Bool {
let regexToMatch = "(?=.*?[A-Z])(?=.*?[0-9]).{\(minCharacters),15}"
let passwordPredicate = NSPredicate(format: "SELF MATCHES %@", regexToMatch)
guard passwordPredicate.evaluate(with: self) == true else {
return false
}
guard self.hasSpecialCharacters() == true else {
return true
}
//The problem is here!
let regexToMatchCaracterSpecial = "(?=.*?[(!@$%^*()_+{};:.,)-])"
let paswordCharacters = NSPredicate(format: "SELF MATCHES %@", regexToMatchCaracterSpecial)
return paswordCharacters.evaluate(with: self) == true
}
func hasSpecialCharacters() -> Bool {
do {
let regex = try NSRegularExpression(pattern: ".*[^A-Za-z0-9].*", options: .caseInsensitive)
if let _ = regex.firstMatch(in: self, options: NSRegularExpression.MatchingOptions.reportCompletion, range: NSMakeRange(0, self.count)) {
return true
}
} catch {
debugPrint(error.localizedDescription)
return false
}
return false
}
}
The error is the next:
2018-09-19 17:03:15.353831-0500 mPaymentsAndLoyaly-Chedraui[47378:9327896] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Can't do regex matching, reason: Can't open pattern U_REGEX_INVALID_RANGE (string Alex1Ç, pattern (?=.*?[(!@$%^*()_-+{};:.,)]), case 0, canon 0)'
i Put the hyphen character but didnt evaluate the condition.