你好,我想提取()之間的文本。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)
。