日々精進

新しく学んだことを書き留めていきます

NSRegularExpression

NSRegularExpressionに正規表現を渡す時に、文字クラス内に「-」をいれても-にマッチしてくれなくて困った。

    NSRegularExpression *regexp =
    [NSRegularExpression regularExpressionWithPattern:@"[a-z+-_]"
                                              options:NSRegularExpressionCaseInsensitive
                                                error:&err];

上記のように文字クラスの途中に-が入ってるとだめで、最後ならマッチした。

    // これならOK
    NSRegularExpression *regexp =
    [NSRegularExpression regularExpressionWithPattern:@"[a-z+_-]"
                                              options:NSRegularExpressionCaseInsensitive
                                                error:&err];