YosiFZ
YosiFZ

Reputation: 7900

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.

Upvotes: 2

Views: 1556

Answers (6)

Rajneesh071
Rajneesh071

Reputation: 31081

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:@""];

Upvotes: 0

Anoop Vaidya
Anoop Vaidya

Reputation: 46543

You can find it by using regex as:

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

Upvotes: 0

Monolo
Monolo

Reputation: 18253

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 *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.

Upvotes: 2

Guy Kogus
Guy Kogus

Reputation: 7341

+ (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];
}

Upvotes: 0

Sulthan
Sulthan

Reputation: 130102

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:@""];

Upvotes: 3

Vishal Kardode
Vishal Kardode

Reputation: 971

Use following regex for removing braces with numbers

\([0-9]+\)

Upvotes: 1

Related Questions