object - c 在URL中截取特定参数的值

时间:2022-10-28 06:43:32
#pragma mark - 获取url特定的参数
-(NSString *) jiexi:(NSString *)CS webaddress:(NSString *)webaddress
{
NSError *error;
NSString *regTags=[[NSString alloc] initWithFormat:@"(^|&|\\?)+%@=+([^&]*)(&|$)",CS];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regTags
options:NSRegularExpressionCaseInsensitive
error:&error]; // 执行匹配的过程
NSArray *matches = [regex matchesInString:webaddress
options:
range:NSMakeRange(, [webaddress length])];
for (NSTextCheckingResult *match in matches) { NSString *tagValue = [webaddress substringWithRange:[match rangeAtIndex:]]; // 分组2所对应的串
return tagValue;
}
return @"";
}