添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

1、方法一:键和值一起替换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- (NSString *) paramValueOfUrl:(NSString *) url withParam:(NSString *) param value:(NSString *)value{

NSError *error;

NSString *targetString = [NSString stringWithFormat:@"%@=%@",param,value];

NSString *regTags = [[NSString alloc] initWithFormat:@"%@=+([\\w]*)",param];

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regTags options:0 error:&error];

NSString *string = [regex stringByReplacingMatchesInString:url
options:0
range:NSMakeRange(0, [url length])
withTemplate:targetString];

return string;
}

2、方法二: 值替换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/// 方法二:
- (NSString *) paramValueOfUrl2:(NSString *) url withParam:(NSString *) param value:(NSString *)value{

NSError *error;

// (^|&|\\?|#)+%@=+([^&|#]*)(#|&|$) : range 取 [match rangeAtIndex:2]
// %@=+([^&|#]*)(#|&|$) : range 取 [match rangeAtIndex:1]
// 通过numberOfRanges获取结果数量,然后分别打印获取值 [url substringWithRange:[match rangeAtIndex:i]]
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) {
NSRange range = [match rangeAtIndex:2];
NSString *tagValue = [url substringWithRange:range]; // 分组2所对应的串
if (![value isEqualToString:tagValue]) {
url = [url stringByReplacingCharactersInRange:range withString:value];
}
}
return url;
}