NSPredicate用于正则表达式模式匹配崩溃

时间:2016-03-12 12:34:11

标签: ios regex swift nspredicate

此代码:

 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',原因:'无法解析格式   字符串" SELF MATCHES [A-Z0-9a-z ._%+ - ] + [A-Za-z0-9 .-] +。[A-Za-z] {2,5}&#34 ;'

4 个答案:

答案 0 :(得分:2)

这是本地处理电子邮件验证的好方法:

Handling email validation using built-in functionality

我也有这种方法用正则表达式处理它:

以下是来自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!

答案 1 :(得分:1)

此外,您还可以使用其他电子邮件正则表达式。

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
}

请检查此正则表达式。

答案 2 :(得分:0)

最后我通过

找到了问题的解决方案
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


}

答案 3 :(得分:0)

使用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)

尽管选择地址验证方法是一个完全不同的故事。