Hello I am new to Swift
, and I am working on a project that requires regex
validation for email validation, here's the code
static func validateEmail(string: String?) -> Style.ValidationResult {
guard let string = string else {
return .init(
isValid: false,
error: ValidationErrors.noEmail
)
}
let format = "^[^!-/[-_{-~]*(?:[0-9A-Za-z](?:[0-9A-Za-z]+|([.])(?!\1)))*([^!-/[-_{-~]){1,256}@[a-zA-Z0-9][a-zA-Z0-9-]{0,64}(\.[a-zA-Z0-9][a-zA-Z0-9-]{0,25})+"
let predicate = NSPredicate(format: "SELF MATCHES %@", format)
return .init(
isValid: predicate.evaluate(with: string),
error: ValidationErrors.noEmail
)
}
When I build the app and actually test this part, it either returns Can't do regex matching, reason: Can't open pattern U_REGEX_MISSING_CLOSE_BRACKET
or simply cannot build.
I am aware that this is due to the escape character, but I have tried many times and still couldn't find out how to solve it, can anyone tell me the rules to conver JavaScript regex to Swift regex