user1960169
user1960169

Reputation: 3653

objective c Regular expression 3

I have a NSString like this.

<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"}

How do I extract this {"success":1,"success_message":"Edit listing success"}part.

Upvotes: 1

Views: 56

Answers (1)

TwoStraws
TwoStraws

Reputation: 13127

As long as that's the general format of your strings, the pattern (\\{[^}]+\\}) ought to do it. Here is some example code to get you started:

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

You should obviously handle errors more gracefully than just ignoring them.

Upvotes: 2

Related Questions