Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在字符串中搜索模式_Iphone_Objective C_Pattern Matching_Nsregularexpression - Fatal编程技术网

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