问题是“\ w +”只用纯文本就可以正常工作。但是,目标是避免将表情符号字符包含为空格。
示例:
"This is some text ".regex("\\w+")
期望的输出:
["This","is","some","text",""]
代码:
extension String {
func regex (pattern: String) -> [String] {
do {
let regex = try NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions(rawValue: 0))
let nsstr = self as NSString
let all = NSRange(location: 0, length: nsstr.length)
var matches : [String] = [String]()
regex.enumerateMatchesInString(self, options: NSMatchingOptions(rawValue: 0), range: all) {
(result : NSTextCheckingResult?, _, _) in
if let r = result {
let result = nsstr.substringWithRange(r.range) as String
matches.append(result)
}
}
return matches
} catch {
return [String]()
}
}
}
上面的代码给出了以下输出:
"This is some text ".regex("\\w+")
// Yields: ["This", "is", "some", "text"]
// Note the are missing.
是编码问题,正则表达式问题还是两者兼而有之?其他答案似乎也表现出同样的问题。
func matchesForRegexInText(regex: String!, text: String!) -> [String] {
do {
let regex = try NSRegularExpression(pattern: regex, options: [])
let nsString = text as NSString
let results = regex.matchesInString(text,
options: [], range: NSMakeRange(0, nsString.length))
return results.map { nsString.substringWithRange($0.range)}
} catch let error as NSError {
print("invalid regex: \(error.localizedDescription)")
return []
}
}
let string = "This is some text "
let matches = matchesForRegexInText("\\w+", text: string)
// Also yields ["This", "is", "some", "text"]
我的错误
\ w +是单词边界
"This is some text \t ".regex("[^ |^\t]+")
// Give correct answer ["This", "is", "some", "text", ""]