Iphone 在字符串中搜索模式
我想搜索字符串中的模式。在下面的代码中,模式字符串“*”可以是任何字符 我从中获得了这个示例代码,但它对我不起作用Iphone 在字符串中搜索模式,iphone,objective-c,pattern-matching,nsregularexpression,Iphone,Objective C,Pattern Matching,Nsregularexpression,我想搜索字符串中的模式。在下面的代码中,模式字符串“*”可以是任何字符 我从中获得了这个示例代码,但它对我不起作用 NSString *string; NSString *pattern; NSRegularExpression *regex; string = @"img=img_1.png or it can be img=img.png"; pattern = @"img=*.png"; regex = [NSRegularExpression regularExp
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]);
}
我希望optput字符串为img_1.png&img.png将模式更改为:
pattern = @"img=(.*?).png";
这种模式可以很好地工作:
NSString *pattern = @"(img=img[\\S]*\\.png)";
比赛内容如下:
0 : {0, 13} - img=img_1.png
1 : {27, 11} - img=img.png
或 另一种模式:
NSString *pattern = @"(img[^=][\\S]*png)";
匹配项为(不含img=零件):
我得到了这个o/p->匹配:img_1.png或者它可以是img=img,其中我想要img_1.png&img.pngThanks,解决了我的问题。
0 : {4, 9} - img_1.png
1 : {31, 7} - img.png