2011-01-12 7 views
3

私は例 オリジナル文字列の括弧NSRegularExpression(正規表現パターン)を使用して文字列を抽出することができません - iphone

を[]の間の文字列を抽出したい:@「これは、[この文字列を取得する]テストです。」

この例では、[この文字列を取得する]という[]の間に文字列を抽出します。

私が何か間違ったことをやっている場合は私に知らせてください私のコード

NSMutableString *predicateString = [[NSMutableString alloc] initWithFormat:@"%@",@"this is a test [to get this string]."]; 

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\[[^\].]*\]" 
        options:NSRegularExpressionCaseInsensitive 
        error:&error]; 


NSMutableArray *rangeArray = [[NSMutableArray alloc] init]; 
__block NSUInteger count = 0; 
[regex enumerateMatchesInString:predicateString options:0 range:NSMakeRange(0, [predicateString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){ 
    NSRange matchRange = [match range]; 
    matchRange.length = matchRange.length+1; 
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 
    NSLog(@"matchRange.location: %d",matchRange.location); 
    NSLog(@"matchRange.length: %d",matchRange.length); 

    if (++count >= 100) *stop = YES; 

}]; 

の下に見つけてください。

ありがとうございました。

答えて

1

表現が間違っているように見えます。スラッシュ(例:@"\\[[^\\].]*\\]")をエスケープする必要があります。

+0

ありがとうMarcelo .....それは働いた。 – D25

関連する問題