こんにちは私は()の間にテキストを抽出したいと思います。例えば括弧内の単語を抽出するSwift正規表現
:
(some text) some other text -> some text
(some) some other text -> some
(12345) some other text -> 12345
括弧の間の文字列の最大長は10個の文字であるべきです。うまく動作
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)
}
}
しかし試合は、次のとおりです:
(TooLongStri) -> nothing matched because 11 characters
は、私が現在持っていることである
(some text) some other text -> (some text)
(some) some other text -> (some)
(12345) some other text -> (12345)
と()もカウントされるため、< = 10と一致していません。
私はそれを解決するために上記のコードをどのように変更できますか?また、長さ情報を保持する正規表現を拡張してif (match.characters.count <= 10)
を削除したいと思います。