I want to search a pattern in a string. In the below code the pattern string ‘*’ can be any character.
I got this sample code from here, but it’s not working for me.
NSString *string;
NSString *pattern;
NSRegularExpression *regex;
string = @"img=img_1.png or it can be img=img.png";
pattern = @"img=*.png";
regex = [NSRegularExpression
regularExpressionWithPattern:pattern
options:NSRegularExpressionCaseInsensitive
error:nil];
NSArray *matches = [regex matchesInString:string
options:0
range:NSMakeRange(0, [string length])];
NSLog(@"matches - %@", matches);
for (NSTextCheckingResult *match in matches)
{
NSRange range = [match rangeAtIndex:1];
NSLog(@"match: %@", [string substringWithRange:range]);
}
I want the optput string to be img_1.png & img.png
Change your pattern to: