objective c正则表达式3

时间:2015-12-17 10:01:08

标签: regex nsstring nsregularexpression

我有NSString这样的人。

<br /><b>Notice</b>:  Undefined variable: success in <b>/var/www/webservice/gcm/setMessage.php</b> on line <b>1225</b><br /><br />

<b>Warning</b>:  pack(): Type H: illegal hex digit ( in <b>/var/www/webservice/gcm/setMessage.php</b> on line <b>1434</b><br /><br />

<b>Warning</b>:  pack(): Type H: illegal hex digit n in <b>/var/www/webservice/gcm/setMessage.php</b> on line <b>1434</b><br /><br />

<b>Warning</b>:  pack(): Type H: illegal hex digit u in <b>/var/www/webservice/gcm/setMessage.php</b> on line <b>1434</b><br /><br />

<b>Warning</b>:  pack(): Type H: illegal hex digit l in <b>/var/www/webservice/gcm/setMessage.php</b> on line <b>1434</b><br /><br />

<b>Warning</b>:  pack(): Type H: illegal hex digit l in <b>/var/www/webservice/gcm/setMessage.php</b> on line <b>1434</b><br /><br />

<b>Warning</b>:  pack(): Type H: illegal hex digit ) in <b>/var/www/webservice/gcm/setMessage.php</b> on line <b>1434</b><br /><br />

 <b>Notice</b>:  Undefined variable: success in <b>/var/www/webservice/gcm/setMessage.php</b> on line 
    <b>1225</b><br />{"success":1,"success_message":"Edit listing success"}

如何提取此{"success":1,"success_message":"Edit listing success"}部分。

1 个答案:

答案 0 :(得分:2)

只要这是字符串的一般格式,模式NSString *test = @"<br /><b>Notice</b>: Undefined variable: success in <b>/var/www/webservice/gcm/setMessage.php</b> on line <b>1225</b><br /><br /><b>Warning</b>: pack(): Type H: illegal hex digit ( in <b>/var/www/webservice/gcm/setMessage.php</b> on line <b>1434</b><br /><br /><b>Warning</b>: pack(): Type H: illegal hex digit n in <b>/var/www/webservice/gcm/setMessage.php</b> on line <b>1434</b><br /><br /><b>Warning</b>: pack(): Type H: illegal hex digit u in <b>/var/www/webservice/gcm/setMessage.php</b> on line <b>1434</b><br /><br /><b>Warning</b>: pack(): Type H: illegal hex digit l in <b>/var/www/webservice/gcm/setMessage.php</b> on line <b>1434</b><br /><br /><b>Warning</b>: pack(): Type H: illegal hex digit l in <b>/var/www/webservice/gcm/setMessage.php</b> on line <b>1434</b><br /><br /><b>Warning</b>: pack(): Type H: illegal hex digit ) in <b>/var/www/webservice/gcm/setMessage.php</b> on line <b>1434</b><br /><br /><b>Notice</b>: Undefined variable: success in <b>/var/www/webservice/gcm/setMessage.php</b> on line <b>1225</b><br />{\"success\":1,\"success_message\":\"Edit listing success\"}"; NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:@"(\\{[^}]+\\})" options:0 error:nil]; NSTextCheckingResult *result = [regex firstMatchInString:test options:0 range:NSMakeRange(0, test.length)]; NSString *match = [test substringWithRange:result.range]; NSLog(@"Matched %@", match); 就应该这样做。以下是一些示例代码,可帮助您入门:

Item No

显然,你应该更优雅地处理错误而不是忽略错误。