用于正则表达式模式匹配崩溃的 NSPredicate
NSPredicate for regex pattern matching crashes
此代码:
internal let emailRegex:String = "[A-Z0-9a-z._%+-]+[A-Za-z0-9.-]+\.[A-Za-z]{2,5}"
let emailText = NSPredicate(format: "SELF MATCHES \(emailRegex)")
return emailText .evaluateWithObject(email)
崩溃错误:
'NSInvalidArgumentException', reason: 'Unable to parse the format
string "SELF MATCHES [A-Z0-9a-z._%+-]+[A-Za-z0-9.-]+.[A-Za-z]{2,5}"'
此外,你的我还有另一个电子邮件正则表达式,你可以使用。
func isValidEmail() -> Bool {
let regex = NSRegularExpression(pattern: "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$",
options: [.CaseInsensitive])
return regex.firstMatchInString(self, options:[],
range: NSMakeRange(0, emailString.characters.count)) != nil
}
请检查这个正则表达式。
这是本机处理电子邮件验证的好方法:
Handling email validation using built-in functionality
我也有这种方法用regex来处理:
这是来自 I Knew How To Validate An Email Address Until I Read The RFC
的正则表达式
import Foundation
let pattern = "^(?!\.)(\"([^\"\r\\]|\\[\"\r\\])*\"|([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$"
let predicate = NSPredicate(format: "SELF MATCHES %@", pattern)
// tests in the format (email, isValid)
let tests = [
("NotAnEmail", false),
("@NotAnEmail", false),
("\"test\\rblah\"@example.com", true),
("\"test\rblah\"@example.com", false),
("\"test\\"blah\"@example.com", true),
("\"test\"blah\"@example.com", false),
("customer/department@example.com", true),
("$A12345@example.com", true),
("!def!xyz%abc@example.com", true),
("_Yosemite.Sam@example.com", true),
("~@example.com", true),
(".wooly@example.com", false),
("wo..oly@example.com", false),
("pootietang.@example.com", false),
(".@example.com", false),
("\"Austin@Powers\"@example.com", true),
("Ima.Fool@example.com", true),
("\"Ima.Fool\"@example.com", true),
("\"Ima Fool\"@example.com", true),
("Ima Fool@example.com", false)]
for (index,(email,isValid)) in tests.enumerate() {
let eval = predicate.evaluateWithObject(email)
if eval == isValid {
print(index, ": VALID!")
}
}
输出:
0 : VALID!
1 : VALID!
2 : VALID!
3 : VALID!
4 : VALID!
5 : VALID!
6 : VALID!
8 : VALID!
10 : VALID!
11 : VALID!
12 : VALID!
13 : VALID!
14 : VALID!
15 : VALID!
17 : VALID!
18 : VALID!
19 : VALID!
最后我通过
找到了问题的解决方案
func isValidEmail(email:String) -> Bool
{
let regex = try! NSRegularExpression(pattern: "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$",
options: [.CaseInsensitive])
return regex.firstMatchInString(email, options:[],
range: NSMakeRange(0, email.characters.count)) != nil
}
NSPredicate 的正确使用方法是
import Foundation
let emailAddress = "mailbox@example.com"
let pattern = "^.+@([A-Za-z0-9-]+\.)+[A-Za-z]{2}[A-Za-z]*$"
let predicate = NSPredicate(format: "SELF MATCHES %@", argumentArray: [pattern])
let isValidEmailAddress = predicate.evaluate(with: emailAddress)
print(isValidEmailAddress)
地址验证方法的选择是完全不同的故事。
此代码:
internal let emailRegex:String = "[A-Z0-9a-z._%+-]+[A-Za-z0-9.-]+\.[A-Za-z]{2,5}"
let emailText = NSPredicate(format: "SELF MATCHES \(emailRegex)")
return emailText .evaluateWithObject(email)
崩溃错误:
'NSInvalidArgumentException', reason: 'Unable to parse the format string "SELF MATCHES [A-Z0-9a-z._%+-]+[A-Za-z0-9.-]+.[A-Za-z]{2,5}"'
此外,你的我还有另一个电子邮件正则表达式,你可以使用。
func isValidEmail() -> Bool {
let regex = NSRegularExpression(pattern: "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$",
options: [.CaseInsensitive])
return regex.firstMatchInString(self, options:[],
range: NSMakeRange(0, emailString.characters.count)) != nil
}
请检查这个正则表达式。
这是本机处理电子邮件验证的好方法:
Handling email validation using built-in functionality
我也有这种方法用regex来处理:
这是来自 I Knew How To Validate An Email Address Until I Read The RFC
的正则表达式import Foundation
let pattern = "^(?!\.)(\"([^\"\r\\]|\\[\"\r\\])*\"|([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$"
let predicate = NSPredicate(format: "SELF MATCHES %@", pattern)
// tests in the format (email, isValid)
let tests = [
("NotAnEmail", false),
("@NotAnEmail", false),
("\"test\\rblah\"@example.com", true),
("\"test\rblah\"@example.com", false),
("\"test\\"blah\"@example.com", true),
("\"test\"blah\"@example.com", false),
("customer/department@example.com", true),
("$A12345@example.com", true),
("!def!xyz%abc@example.com", true),
("_Yosemite.Sam@example.com", true),
("~@example.com", true),
(".wooly@example.com", false),
("wo..oly@example.com", false),
("pootietang.@example.com", false),
(".@example.com", false),
("\"Austin@Powers\"@example.com", true),
("Ima.Fool@example.com", true),
("\"Ima.Fool\"@example.com", true),
("\"Ima Fool\"@example.com", true),
("Ima Fool@example.com", false)]
for (index,(email,isValid)) in tests.enumerate() {
let eval = predicate.evaluateWithObject(email)
if eval == isValid {
print(index, ": VALID!")
}
}
输出:
0 : VALID!
1 : VALID!
2 : VALID!
3 : VALID!
4 : VALID!
5 : VALID!
6 : VALID!
8 : VALID!
10 : VALID!
11 : VALID!
12 : VALID!
13 : VALID!
14 : VALID!
15 : VALID!
17 : VALID!
18 : VALID!
19 : VALID!
最后我通过
找到了问题的解决方案func isValidEmail(email:String) -> Bool
{
let regex = try! NSRegularExpression(pattern: "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$",
options: [.CaseInsensitive])
return regex.firstMatchInString(email, options:[],
range: NSMakeRange(0, email.characters.count)) != nil
}
NSPredicate 的正确使用方法是
import Foundation
let emailAddress = "mailbox@example.com"
let pattern = "^.+@([A-Za-z0-9-]+\.)+[A-Za-z]{2}[A-Za-z]*$"
let predicate = NSPredicate(format: "SELF MATCHES %@", argumentArray: [pattern])
let isValidEmailAddress = predicate.evaluate(with: emailAddress)
print(isValidEmailAddress)
地址验证方法的选择是完全不同的故事。