2013-05-16 46 views
1

我遇到了與iOS上的正則表達式匹配的問題,嘗試匹配"avatar_urls":{...}並崩潰。正則表達式匹配成功here,所以我認爲這可能是iOS的怪癖。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} 
+1

你不應該只是[NSJSONSerialization(http://developer.apple.com/library/解析此IOS /#文檔/基金/參考/ NSJSONSerialization_Class /參考/的reference.html)? – Rob

回答

3

波浪括號是特殊(table 2):

{n} | Match exactly n times. 

逃脫它們。

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

或者,甚至更好的是,使用適當的JSON解析器。

+0

它應該是在捲髮前面的雙反斜槓。 – Brigham

1

如果您檢索這個應答分爲NSData,你也可以這樣解析爲:

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); 
}