如何使用 NSRegularExpression 提取“badgeCount”标签之间的值“6”。以下是来自服务器的响应:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><badgeCount>6</badgeCount><rank>2</rank><screenName>myName</screenName>
以下是我尝试但没有成功的代码。实际上它进入其他部分并打印“正则表达式的值为零”:
NSString *responseString = [[NSString alloc] initWithBytes:[responseDataForCrntUser bytes] length:responseDataForCrntUser.length encoding:NSUTF8StringEncoding];
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=badgeCount>)(?:[^])*?(?=</badgeCount)" options:0 error:&error];
if (regex != nil) {
NSTextCheckingResult *firstMatch = [regex firstMatchInString:responseString options:0 range:NSMakeRange(0, [responseString length])];
NSLog(@"NOT NIL");
if (firstMatch) {
NSRange accessTokenRange = [firstMatch rangeAtIndex:1];
NSString *value = [urlString substringWithRange:accessTokenRange];
NSLog(@"Value: %@", value);
}
}
else
NSLog(@"Value of regex is nil");
如果您能提供示例代码,将不胜感激。
注意:我不想使用 NSXMLParser。