Reputation: 163
I have a little problem with regex in iOS.
I want to delete each tag <a>
in NSString.
I made this code but it doesn't stop at first occurence of .
NSString *regexStr = @"<a (.+)>(.+)</a>";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:NULL];
prodDescritpion = [regex stringByReplacingMatchesInString:prodDescritpion options:0 range:NSMakeRange(0, [prodDescritpion length]) withTemplate:@"$2"];
Thanks you !
Upvotes: 0
Views: 497
Reputation: 5452
the + operator is greedy, that means it stops at the last occurrence it finds. One solution could also be to use it in the non greedy version (ie it stops at the first occurrence)
NSString *regexStr = @"<a.+?>.+?</a>";
Upvotes: 0
Reputation: 163
I have find a solution
NSString *regexStr = @"<a ([^>]+)>([^>]+)</a>";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:NULL];
prodDescritpion = [regex stringByReplacingMatchesInString:prodDescritpion options:0 range:NSMakeRange(0, [prodDescritpion length]) withTemplate:@"$2"];
It works fine !
Upvotes: 3