1

我在 iOS 上匹配正则表达式时遇到问题,尝试匹配"avatar_urls":{...}和折叠。正则表达式在这里匹配成功,所以我认为这可能是一个 iOS 怪癖。

代码:

NSMutableString *response = [NSMutableString stringWithString:[request responseString]];
NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:@"\"avatar_urls\":{[^}]+}" options:(NSRegularExpressionAllowCommentsAndWhitespace | NSRegularExpressionDotMatchesLineSeparators) error:&error];
NSInteger numMatches = [regex replaceMatchesInString:response options:NSMatchingReportCompletion range:NSRangeFromString(response) withTemplate:@"\"avatar_urls\"{}"];

错误:

The operation couldn’t be completed. (Cocoa error 2048.) 
{
    NSInvalidValue = "\"avatar_urls\":{[^}]+}";
}

示例字符串:

{"user":{"id":1001090,"login":"Julesndiamonds","path":"/julesndiamonds","restful_url":"http://8tracks.com/users/1001090","avatar_urls":{"sq56":"http://imgix.8tracks.com/avatars/001/001/090/76935.original.png?fm=jpg&q=65&sharp=15&vib=10&w=56&h=56&fit=crop","sq72":"http://imgix.8tracks.com/avatars/001/001/090/76935.original.png?fm=jpg&q=65&sharp=15&vib=10&w=72&h=72&fit=crop","sq100":"http://imgix.8tracks.com/avatars/001/001/090/76935.original.png?fm=jpg&q=65&sharp=15&vib=10&w=100&h=100&fit=crop","max200":"http://imgix.8tracks.com/avatars/001/001/090/76935.original.png?fm=jpg&q=65&sharp=15&vib=10&w=200&h=200&fit=max","max250w":"http://imgix.8tracks.com/avatars/001/001/090/76935.original.png?fm=jpg&q=65&sharp=15&vib=10&w=250&h=250&fit=max","original":"http://imgix.8tracks.com/avatars/001/001/090/76935.original.png?fm=jpg&q=65&sharp=15&vib=10&"},"location":"Potrero Hill, San Francisco, CA, US","bio_html":"<p>If only I could pay my bills in smiles. I love rocking out to mix tapes while I work as the 8tracks Community Manager. </p>\n\n<p>Love thrifting, eating all kinds of food, and cuddling up with a good book and a glass of wine. </p>","website":"http://www.lomatika.com","designation":"staff","public_mixes_count":5,"follows_count":127,"followers_count":74,"followed_by_current_user":false,"presets":["chill+rap","ambient+electronic","chillstep+sex","electrofunk","hip hop+workout","r&b+soul","brazil+summer","country+love"],"preset_smart_ids":["tags:chill+rap","tags:ambient+electronic","tags:chillstep+sex","tags:electrofunk","tags:hip_hop+workout","tags:r&b+soul","tags:brazil+summer","tags:country+love"],"ios_subscribed":false,"connected_facebook_user":{"id":1050450021,"uid":1050450021,"post_listens":true,"post_favs":true,"post_likes":true,"post_mixes":true,"granted_publish_actions":true,"explicitly_set_permissions":true},"tracking_settings":{"mixpanel_enabled":null,"mixpanel_super_properties":null,"mixpanel_identity":null},"subscribed":false,"web_safe_browse":false,"mobile_safe_browse":false,"user_token":"1001090;9f6d99bf40c22b646410a66a723b4af6860eab2c"},"status":"200 OK","errors":null,"notices":null,"logged_in":true,"api_version":2.1}
4

2 回答 2

3

波浪形大括号是特殊的(表 2):

{n} | Match exactly n times.

逃离他们。

@"\"avatar_urls\":\\{[^}]+\\}"

或者,更好的是,使用适当的 JSON 解析器。

于 2013-05-16T22:02:18.860 回答
1

如果您将此响应检索到 aNSData中,您还可以将其解析为:

NSError *error = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data
                                                           options:0
                                                             error:&error];

if (!error)
{
    NSDictionary *user = dictionary[@"user"];
    NSDictionary *avatarUrls = user[@"avatar_urls"];
    NSLog(@"%@", avatarUrls);
}
else
{
    NSLog(@"%s: error = %@", __FUNCTION__, error);
}
于 2013-05-16T22:08:37.663 回答