NSPredicate vs NSRegularExpression结果差异

时间:2018-01-18 20:58:15

标签: ios regex swift nspredicate nsregularexpression

我在这里遇到一种奇怪的情况,并想问其他人是否在那里看到我的问题。

所以,我尝试使用正则表达式来检查URL是否有效使用此模式:

let pattern = "^(http|https):\\/\\/(.+\\.)(whatever|whenever)\\.com(.+\\/)*platform/outsource"

要测试的网址是:

let strTest = "https://int.me2.whatever.com/RandomStuff;WbSess=mhLkhgmHWGGwDMYQ1sqFvgCp0FNGhhkdhTpv0kpYJvQggST3ll55!217115774!NONE?additional=1&popup=/platform/outsource/&action=openext"

因此,如果我使用NSRegularExpression进行检查,则返回1匹配;但如果我使用NSPredicate,它说它不匹配......

let regex = try! NSRegularExpression(pattern: pattern, options: [])
let matches = regex.matches(in: strTest, options: [], range: NSRange(location: 0, length: strTest.characters.count))
let regexCondition:Bool = NSPredicate(format: "SELF MATCHES %@", regex.pattern).evaluate(with: strTest)

print("Regex Matches: \(matches.count)")
print("Predicate Condition: \(regexCondition)")

结果是:

Regex Matches: 1
Predicate Condition: false

提前致谢。

0 个答案:

没有答案