I want to match a string with a regex in Swift. I am following the approach described here.
Typically this would work like this (as evaluated in a Xcode playground):
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
So far so good. Now let us take two strings which contain characters consisting of more than one Unicode scalar like so (as evaluated in a Xcode playground):
var str3 = "✔️"
var regex2 = "[✖️]"
"✔️" == "✖️" // false - the strings are not equal
str3.rangeOfString(regex2, options:.RegularExpressionSearch) != nil // true - there is a match!
I wouldn't expect a match when I try to find "✖️"
in "✔️"
, but because "\u{2714}"+"\u{FE0F}" == "✔️"
and "\u{2716}"+"\u{FE0F}" == "✖️"
, then "\u{FE0F}"
is found in both and that gives a match.
How would you perform the match?