Come estrarre il valore "6" tra i tag "badgeCount" utilizzando NSRegularExpression. Di seguito è riportata la risposta dal server:NSRegularExpression per estrarre il testo tra due tag XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><badgeCount>6</badgeCount><rank>2</rank><screenName>myName</screenName>
seguito è il codice che ho provato ma non ottenere il successo. In realtà si va in parte il resto e stampe "Valore della regex è pari a zero":
NSString *responseString = [[NSString alloc] initWithBytes:[responseDataForCrntUser bytes] length:responseDataForCrntUser.length encoding:NSUTF8StringEncoding];
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=badgeCount>)(?:[^])*?(?=</badgeCount)" options:0 error:&error];
if (regex != nil) {
NSTextCheckingResult *firstMatch = [regex firstMatchInString:responseString options:0 range:NSMakeRange(0, [responseString length])];
NSLog(@"NOT NIL");
if (firstMatch) {
NSRange accessTokenRange = [firstMatch rangeAtIndex:1];
NSString *value = [urlString substringWithRange:accessTokenRange];
NSLog(@"Value: %@", value);
}
}
else
NSLog(@"Value of regex is nil");
Se si potesse fornire il codice di esempio che sarebbe molto apprezzato.
NOTA: non desidero utilizzare NSXMLParser.
Perché non ti vuoi se un parser xml analizza xml? – Mat
E che cosa hai provato finora? – Mat
Nessuna necessità di NSXMLParser per estrarre solo un paio di valori ... Ho ottenuto l'espressione regolare necessaria "(? <=badgeCount>) (?: [^]) *? (? = BadgeCount)" utilizzando http: // gskinner.com/RegExr/ strumento online ma impossibile utilizzare la stessa espressione in NSRegular Expression ... – Prazi