[英]NSString remove Brackets
I am using this function to remove Brackets and inside them: 我正在使用此功能删除支架及其内部:
+(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;
}
I called it twice to remove twice ,And it remove it perfectly. 我叫过两次,两次删除,然后完美地删除了它。
The issue is when i have string like this: 问题是当我有这样的字符串时:
Mystring(blablabla)(*).mp3
* - Is a number. can be 0-999
And i want to remove only the (*). 而且我只想删除(*)。
How i can implement it? 我该如何实施?
Edit: 编辑:
The string can be : 该字符串可以是:
mystring(bla bla)(1).mp3
mystring(bla bla)(1123).mp3
mystring(99).mp3
mystring(9).mp3
mystring.mp3
mystring(bla bla).mp3
And i need to remove the (number)
if it's exist. 而且我需要删除(number)
如果存在)。
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:@""];
NSString
has a very useful method, stringByReplacingOccurrencesOfString:withString:options:range:
, which does exactly what you want if you use regular expression search as an option: NSString
有一个非常有用的方法stringByReplacingOccurrencesOfString:withString:options:range:
如果您使用正则表达式搜索作为选项,该方法完全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)];
You don't have to define the pattern in a variable by itself, but I find that it enhances readability. 您不必自己在变量中定义模式,但我发现它可以提高可读性。
The pattern is the same as the one used for NSRegularExpression
, so you can read about it there. 该模式与用于NSRegularExpression
的模式相同,因此您可以在NSRegularExpression
进行阅读。
使用以下正则表达式删除带数字的花括号
\([0-9]+\)
Try this it will work like this (*) 试试这个,它将像这样(*)
NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:@"\\([0-9]*\\)" options:0 error:NULL];
NSString *result = [regexp stringByReplacingMatchesInString:string
options:0
range:NSMakeRange(0, [string length])
withTemplate:@""];
+ (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];
}
You can find it by using regex as: 您可以使用regex找到它:
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);
Output: 输出:
abc(bud).mp3 ABC(芽).MP3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.