我想在Swift中将字符串与正则表达式匹配。我遵循here描述的方法。
通常这会像这样工作(在Xcode游乐场中评估):
var str1 = "hello"
var str2 = "bye"
var regex1 = "[abc]"
str1.rangeOfString(regex1, options:.RegularExpressionSearch) != nil // false - there is no match
str2.rangeOfString(regex1, options:.RegularExpressionSearch) != nil // true - there is a match
到目前为止一切顺利。现在让我们取两个字符串,其中包含由多个Unicode标量组成的字符(如在Xcode游乐场中评估的那样):
var str3 = "✔️"
var regex2 = "[✖️]"
"✔️" == "✖️" // false - the strings are not equal
str3.rangeOfString(regex2, options:.RegularExpressionSearch) != nil // true - there is a match!
当我尝试在"✖️"
中找到"✔️"
时,我不希望匹配,但因为"\u{2714}"+"\u{FE0F}" == "✔️"
和"\u{2716}"+"\u{FE0F}" == "✖️"
,然后找到了"\u{FE0F}"
两者都给出了匹配。
你将如何进行比赛?
答案 0 :(得分:1)
深入研究@stribizhev提供的link我已经想出了这个(在Xcode游乐场中评估):
var str1 = "hello"
var str2 = "bye"
var str3 = "✔️"
var regex1 = "[abc]"
var regex2 = "[✖️]"
let matcher1 = try! NSRegularExpression(pattern: regex1, options: NSRegularExpressionOptions.CaseInsensitive)
let matcher2 = try! NSRegularExpression(pattern: regex2, options: NSRegularExpressionOptions.CaseInsensitive)
matcher1.numberOfMatchesInString(str1, options: NSMatchingOptions.ReportCompletion, range: NSMakeRange(0, str1.characters.count)) != 0 // false - does not match
matcher1.numberOfMatchesInString(str2, options: NSMatchingOptions.ReportCompletion, range: NSMakeRange(0, str2.characters.count)) != 0 // true - matches
matcher2.numberOfMatchesInString(str3, options: NSMatchingOptions.ReportCompletion, range: NSMakeRange(0, str3.characters.count)) != 0 // false - does not match
这适用于XCode 7.1和Swift 2.1