objective c 正则表达式 3
objective c Regular expression 3
我有一个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"}
部分。
只要这是字符串的一般格式,模式 (\{[^}]+\})
就应该可以。下面是一些帮助您入门的示例代码:
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);
你显然应该更优雅地处理错误,而不是忽略它们。
我有一个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"}
部分。
只要这是字符串的一般格式,模式 (\{[^}]+\})
就应该可以。下面是一些帮助您入门的示例代码:
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);
你显然应该更优雅地处理错误,而不是忽略它们。