你好,我想提取()之间的文本。Swift正则表达式用于在括号之间提取单词
例如:
(some text) some other text -> some text
(some) some other text -> some
(12345) some other text -> 12345
括号之间的字符串应该是10个字符的最大长度。
(TooLongStri) -> nothing matched because 11 characters
我有什么目前:
let regex = try! NSRegularExpression(pattern: "\\(\\w+\\)", options: [])
regex.enumerateMatchesInString(text, options: [], range: NSMakeRange(0, (text as NSString).length))
{
(result, _, _) in
let match = (text as NSString).substringWithRange(result!.range)
if (match.characters.count <= 10)
{
print(match)
}
}
其作品很好,但比赛有:
(some text) some other text -> (some text)
(some) some other text -> (some)
(12345) some other text -> (12345)
因为()也被计算在内不符合< = 10。
我如何更改上面的代码来解决这个问题?我还想通过扩展正则表达式来保留长度信息来删除if (match.characters.count <= 10)
。