Xcode 8.3.3
我写了一个正则表达式方法来获取字符串的所有提及。
func getMentions() -> [String] {
let pattern = "\\B@[A-Za-z0-9]+"
do {
let regex = try NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options(rawValue: 0))
let nsstr = self as NSString
let all = NSRange(location: 0, length: nsstr.length)
var matches : [String] = [String]()
regex.enumerateMatches(in: self, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: all) {
(result : NSTextCheckingResult?, _, _) in
if let r = result {
let result = nsstr.substring(with: r.range) as String
matches.append(result)
}
}
Log.info(matches)
return matches
} catch {
return []
}
}
这曾经很好用。 但是,当我运行它时,我得到空的匹配。刚才有些事情得到了更新吗?
我也试过这个:
let pattern = "\\B@[A-Za-z0-9]+\\b"