2
我正在開發一個iOS應用程序,並且需要從括號中刪除括號中的所有文本。例如:「看看這個圖片[960×640]」應該是「看看這個圖片」使用正則表達式刪除括號之間的文本
我的代碼工作正常,如果只有一個組括號,但如果有多個,它只是刪除了第一盤。
+ (NSString *)stringWithoutBrackets:(NSString *)input{
NSString *expression = @"\\[[\\w]+\\]";
while ([input rangeOfString:expression options:NSRegularExpressionSearch|NSCaseInsensitiveSearch].location!=NSNotFound){
input = [input stringByReplacingOccurrencesOfString:expression withString:@"" options:NSRegularExpressionSearch|NSCaseInsensitiveSearch range:NSMakeRange(0, [input length])];
}
return input;
}
謝謝。我如何修改這個以去掉括號而不是括號? – Chase 2013-03-17 19:24:15
使用@「\\\(\\ w + \\\)」 – 2013-03-18 04:31:59
@NickC,我如何刪除**或^^之間的子串? – keshav 2015-10-27 09:21:05