Reputation: 64
Basically, my string looks like this:
@"{{attendee.prefix}} {{attendee.firstname}} {{attendee.lastname}}, fwf<br /><span style="font-size:14px;">lalallasgabab {{attendee.weg2g}} {{attendee.5236t2gsg}} {{attendee.ticket_no}} agagawfbeagabs</span>"
I am trying to extract all the string which encapsulated by 2 curly braces:
[ {{attendee.prefix}}, {{attendee.firstname}}, {{attendee.lastname}}, {{attendee.weg2g}}, {{attendee.5236t2gsg}}, {{attendee.ticket_no}} ]
I have tried these regex, but it always return 1 match if not the whole string.
@"(\\{\\{.*\\}\\})"
-> return the whole string
@"\\{\\{[^}]*+\\}\\}"
-> only match {{attendee.firstname}}
@"\\b\\{\\{[^}]*+\\}\\}\\b"
-> only match {{attendee.prefix}}
Here is my code:
NSString *myString = @"{{attendee.prefix}} {{attendee.firstname}} {{attendee.lastname}}, fwf<br /><span style="font-size:14px;">lalallasgabab {{attendee.weg2g}} {{attendee.5236t2gsg}} {{attendee.ticket_no}} agagawfbeagabs</span>"
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\{\\{[^}]*+\\}\\}" options:NSRegularExpressionCaseInsensitive error:nil];
NSRange visibleTextRange = NSMakeRange(0, myString.length);
NSArray *matches = [regex matchesInString:myString options:NSMatchingAnchored range:visibleTextRange];
for (NSTextCheckingResult *match in matches)
{
NSLog(@"%@: Match - %@", [self class], [myString substringWithRange:match.range]);
}
I have tried using [match rangeAtIndex:index] but still return the same thing, sometime it is out of bound because the match result is only 1.
Appreciate any help here. Thanks.
PS: I am new to Objective-C and RegEx, so pardon this question.
Upvotes: 2
Views: 1296
Reputation: 41838
To iterate over all matches like {{this}}
, use this:
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\{\\{[^}]*\\}\\}" options:0 error:&error];
NSArray *matches = [regex matchesInString:subject options:0 range:NSMakeRange(0, [subject length])];
NSUInteger matchCount = [matches count];
if (matchCount) {
for (NSUInteger matchIdx = 0; matchIdx < matchCount; matchIdx++) {
NSTextCheckingResult *match = [matches objectAtIndex:matchIdx];
NSRange matchRange = [match range];
NSString *result = [subject substringWithRange:matchRange];
}
}
else { // Nah... No matches.
}
Upvotes: 1
Reputation: 64
I managed to answer my own question, by using different ways of enumerating the result:
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\{\\{[^}]*+\\}\\}" options:NSRegularExpressionCaseInsensitive error:nil];
NSString *myString = @"{{attendee.prefix}} {{attendee.firstname}} {{attendee.lastname}}, fwf<br /><span style='font-size:14px;'>lalallasgabab {{attendee.weg2g}} {{attendee.5236t2gsg}} {{attendee.ticket_no}} agagawfbeagabs</span>";
[regex enumerateMatchesInString:myString
options:0
range:NSMakeRange(0, [myString length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSLog(@"%@: Match - %@", [self class], [self.duplicateBody substringWithRange:result.range]);
}];
With the code above, I am able to iterate through each of the matched string, which is exactly what I wanted.
Upvotes: 0