我正在使用此功能删除Brackets并在其中:
+(NSString*)removeCharsBetweenBrackets:(NSString*)str {
NSRange range = [str rangeOfString:@"("];
if (range.location != NSNotFound) {
NSRange range2 = [str rangeOfString:@")"];
if (range2.location != NSNotFound) {
NSString *str1 = [str substringToIndex:range.location];
NSString *str2 = [str substringFromIndex:range2.location + 1];
str = [NSString stringWithFormat:@"%@ %@",str1,str2];
}
}
range = [str rangeOfString:@"["];
if (range.location != NSNotFound) {
NSRange range2 = [str rangeOfString:@"]"];
if (range2.location != NSNotFound) {
NSString *str1 = [str substringToIndex:range.location];
NSString *str2 = [str substringFromIndex:range2.location + 1];
str = [NSString stringWithFormat:@"%@ %@",str1,str2];
}
}
return str;
}
我打了两次去除两次,它完全删除了它。
问题是我有这样的字符串:
Mystring(blablabla)(*).mp3
* - Is a number. can be 0-999
我想只删除(*)。
我如何实现它?
编辑:
字符串可以是:
mystring(bla bla)(1).mp3
mystring(bla bla)(1123).mp3
mystring(99).mp3
mystring(9).mp3
mystring.mp3
mystring(bla bla).mp3
如果它存在,我需要删除(number)
。
答案 0 :(得分:3)
NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:@"\\([0-9]{1,3}\\)" options:0 error:NULL];
NSString *result = [regexp stringByReplacingMatchesInString:string
options:0
range:NSMakeRange(0, [string length])
withTemplate:@""];
答案 1 :(得分:2)
NSString
有一个非常有用的方法stringByReplacingOccurrencesOfString:withString:options:range:
,如果您使用正则表达式搜索作为选项,它会完全符合您的要求:
NSString *string = ... // your string here;
NSString *pattern = @"\\(\\d+?\\)"; // Match one or more digits within a pair of brackets
NSString *cleaned = [string stringByReplacingOccurrencesOfString: pattern
withString: @""
options: NSRegularExpressionSearch
range: NSMakeRange(0, string.length)];
您不必单独在变量中定义模式,但我发现它增强了可读性。
该模式与用于NSRegularExpression
的模式相同,因此您可以在那里阅读。
答案 2 :(得分:1)
使用以下正则表达式删除带数字的大括号
\([0-9]+\)
答案 3 :(得分:0)
试试这个就像这样(*)
NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:@"\\([0-9]*\\)" options:0 error:NULL];
NSString *result = [regexp stringByReplacingMatchesInString:string
options:0
range:NSMakeRange(0, [string length])
withTemplate:@""];
答案 4 :(得分:0)
+ (NSString *)removeCharsBetweenBrackets:(NSString *)str
{
NSMutableString *strM = [str mutableCopy];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\\(\\d+\\))"
options:0
error:NULL];
NSArray *matches = [regex matchesInString:str options:0 range:NSMakeRange(0, [str length])];
// Start from the back, since we're changing the size of the string.
for (NSTextCheckingResult *result in [matches reverseObjectEnumerator])
{
[strM replaceCharactersInRange:[result range] withString:@"()"];
}
// Return an immutable version
return [strM copy];
}
答案 5 :(得分:0)
您可以使用正则表达式找到它:
NSString *string = @"abc(bud)(2).mp3";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\([0-9]\\)"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@""];
NSLog(@"%@", modifiedString);
输出
ABC(芽).MP3