转自http://www.cnblogs.com/PleaseInputEnglish/archive/2013/12/25/3489707.html
修改了一下,代码如下
- (NSString *)query:(NSString *)key fromUrlStr:(NSString *)urlStr
{
NSError *error;
NSString *regTags=[[NSString alloc] initWithFormat:@"(^|&|\\?)+%@=+([^&]*)(&|$)", key];
NSRegularExpression *regEx = [NSRegularExpression regularExpressionWithPattern:regTags
options:NSRegularExpressionCaseInsensitive
error:&error];
// 执行匹配的过程
NSArray *matches = [regEx matchesInString:urlStr
options:0
range:NSMakeRange(0, [urlStr length])];
for (NSTextCheckingResult *match in matches) {
NSString *tagValue = [urlStr substringWithRange:[match rangeAtIndex:2]];
return tagValue;
}
return @"";
}