カッコ内の文字列の一部を削除しようとしています。NSRegularExpressionを使用してカッコ内のテキストを削除するにはどうすればよいですか?
例として、文字列が"(This should be removed) and only this part should remain"
の場合、NSRegularExpressionを使用した後は"and only this part should remain"
になります。
私はこのコードを持っていますが、何も起こりません。私はRegExr.comで正規表現コードをテストして正しく動作します。助けていただければ幸いです。
NSString *phraseLabelWithBrackets = @"(test text) 1 2 3 text test";
NSError *error = NULL;
NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:@"/\\(([^\\)]+)\\)/g" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *phraseLabelWithoutBrackets = [regexp stringByReplacingMatchesInString:phraseLabelWithBrackets options:0 range:NSMakeRange(0, [phraseLabelWithBrackets length]) withTemplate:@""];
NSLog(phraseLabelWithoutBrackets);
これをチェックしてください。 http://stackoverflow.com/questions/3741279/how-do-you-remove-parentheses-words-within-a-string-using-nsregularexpression?rq=1 –