当用NSRegularExpression去正则匹配带有表情emoji的字符串的时候,会出现匹配不全的情况
例:
let string = "😳😳😳😳😳😳 [gggg] [gggg] [gggg] [gggg]"
let pattern = "\\[.*?\\]"
if let regularExpression = try? NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options.dotMatchesLineSeparators) {
var matches = regularExpression.matches(in: text, options: NSRegularExpression.MatchingOptions.withTransparentBounds, range: NSRange(location: 0, length: string.count))
print(matches)
}
只会打印出
明显是emoji的锅,只要我们改一下range的length就行
let range = NSRange(location: 0, length: string.utf16.count)
就可以正确打印出:
END