Ios7 从NSString打印特定值

Ios7 从NSString打印特定值,ios7,nsstring,Ios7,Nsstring,值:急救=“我的服务器部件”,qop=“接受”,nonce=“NyeRat567We” 我只想获取并打印nonce您需要正则表达式来提取它: NSDictionary* headers; = [(NSHTTPURLResponse *)response allHeaderFields]; NSString *str;= [headers objectForKey:@"Www-Authenticate"]; NSLog(@"value:%@",str); 你能告诉我你想要的最终结果是什么吗?NS

值:急救=“我的服务器部件”,qop=“接受”,nonce=“NyeRat567We”


我只想获取并打印nonce

您需要正则表达式来提取它:

NSDictionary* headers; = [(NSHTTPURLResponse *)response allHeaderFields];
NSString *str;= [headers objectForKey:@"Www-Authenticate"];
NSLog(@"value:%@",str);

你能告诉我你想要的最终结果是什么吗?NSLog(@“nonce值是我想要的:%@”,nonce);如此寻找子串。这有很多问题。但我想知道
[headers objectForKey:@“Www Authenticate”]
是否不是
NSDictionary
。你的答案很完美,我认为你的答案是正确的……但有一点。你的nslog方法是打印NyeRat567We,但我想打印为nonce=“NyeRat567We”。请再次帮助我。感谢NSTextCheckingResult包含两个结果:第一个是与整个正则表达式匹配的字符串,第二个是与括号内容匹配的字符串。将
[match rangeAtIndex:1]
更改为
[match rangeAtIndex:0]
以提取整个匹配项。
NSError *error=nil;

NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:@"nonce=\"(.*)\"" options:0 error:&error];

NSTextCheckingResult *match = [regex firstMatchInString:str options:0 range:NSMakeRange(0, [str length])];

NSString *nonce = [str substringWithRange:[match rangeAtIndex:1]];

NSLog(@"%@",nonce);