NSString - How to get a portion of a string surrounded by curly braces

My answer would be as follows: {group} community announced: {announcement}

.

Based on the text inside {}

, I have to query the dictionary. I tried to use

componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"{}"]

      

but it returns a lot of details:

(
    "",
    group,
    " community announced: ",
    announcement,
    ""
)

      

So how can I exactly get the characters inside {}

in the array.

+3


source to share


3 answers


Usage NSRegularExpression

:

NSString *string = @"{group} community announced: {announcement}";

NSMutableArray *array = [[NSMutableArray alloc] init];

NSString *pattern = @"\\{((.|\n)*?)\\}";
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];
if (!error)
{
    NSArray *allMatches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
    for (NSTextCheckingResult *aMatch in allMatches)
    {
        NSRange matchRange = [aMatch range];
        NSString *foundString = [string substringWithRange:NSMakeRange(matchRange.location+1, matchRange.length-2)];
        [array addObject:foundString];
    }
}
NSLog(@"Array: %@", array);

      



With the exit:

Array: (group, declaration)

+4


source


Try this nested method call

NSString *myString = @"{group} community announced: {announcement}";

[self logTheSplittedString:myString];

- (void)logTheSplittedString:(NSString *)targetString
{
    NSRange start = [targetString rangeOfString:@"{"];
    NSRange end = [targetString rangeOfString:@"}"];

    NSString *betweenBraces1 = @"";

    if (start.location != NSNotFound && end.location != NSNotFound && end.location > start.location)
    {
        betweenBraces1 = [targetString substringWithRange:NSMakeRange(start.location+1, end.location-(start.location+1))];
        NSLog(@"%@",betweenBraces1);
    }

    targetString = [targetString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"{%@}",betweenBraces1] withString:@""];

    NSRange startNext = [targetString rangeOfString:@"{"];
    NSRange endNext = [targetString rangeOfString:@"}"];

    if (startNext.length != 0 && endNext.length != 0)
    {
        [self logTheSplittedString:targetString];
    }
}

      



Output

group
Announcement

0


source


You can use regex to search for string between '{text}' Refer to the following code

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"{(.*)}" options:NSRegularExpressionCaseInsensitive error:&error];
    [regex enumerateMatchesInString:responceString options:0 range:NSMakeRange(0, [responceString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
        // detect
        NSString *strFind1 = [responceString substringWithRange:[match rangeAtIndex:1]];
        //log
        NSLog(@"%@",strFind1);
    }];

      

0


source







All Articles