+ (NSString *) paramValueOfUrl:(NSString *) url withParam:(NSString *) param{
NSError *error;
NSString *regTags=[[NSString alloc] initWithFormat:@"(^|&|\\?)+%@=+([^&]*)(&|$)",param];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regTags
options:NSRegularExpressionCaseInsensitive
error:&error];
// 执行匹配的过程
NSArray *matches = [regex matchesInString:url
options:0
range:NSMakeRange(0, [url length])];
for (NSTextCheckingResult *match in matches) {
NSString *tagValue = [url substringWithRange:[match rangeAtIndex:2]]; // 分组2所对应的串
return tagValue;
}
return nil;
}转载于:https://www.cnblogs.com/skyry/p/5114493.html
本文介绍了一种使用Objective-C从URL中解析特定参数的方法。通过正则表达式匹配和NSRegularExpression类,可以有效地从URL字符串中提取指定参数的值。这种方法对于处理网页链接中的查询参数非常有用。
448

被折叠的 条评论
为什么被折叠?



