2011-10-05 14 views
7

के बीच टेक्स्ट निकालने के लिए NSRegularExpression का उपयोग करके "बैजकाउंट" टैग के बीच मान "6" निकालने के लिए कैसे करें। सर्वर से प्रतिक्रिया निम्नलिखित है:NSRegularExpression दो एक्सएमएल टैग

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><badgeCount>6</badgeCount><rank>2</rank><screenName>myName</screenName> 

निम्नलिखित कोड मैंने कोशिश की लेकिन सफलता प्राप्त नहीं की। आप नमूना कोड है कि बहुत सराहना की जाएगी प्रदान कर सकता है, तो

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

: वास्तव में यह किसी और हिस्से और प्रिंट में चला जाता है "regex का मूल्य नहीं के बराबर है।"

नोट: मैं NSXMLParser का उपयोग नहीं करना चाहता हूं।

+0

आप क्यों नहीं चाहते हैं xml पार्सर को xml पार्स करने के लिए? – Mat

+0

और आपने अभी तक क्या प्रयास किया है? – Mat

+0

कुछ मूल्यों को निकालने के लिए NSXMLParser की कोई ज़रूरत नहीं है ... मुझे आवश्यक नियमित अभिव्यक्ति मिली है ("<=badgeCount>) (?: [^]) *? (? = Prazi

उत्तर

18

उदाहरण:

NSString *xml = @"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><badgeCount>6</badgeCount><rank>2</rank><screenName>myName</screenName>"; 
NSString *pattern = @"<badgeCount>(\\d+)</badgeCount>"; 

NSRegularExpression *regex = [NSRegularExpression 
             regularExpressionWithPattern:pattern 
             options:NSRegularExpressionCaseInsensitive 
             error:nil]; 
NSTextCheckingResult *textCheckingResult = [regex firstMatchInString:xml options:0 range:NSMakeRange(0, xml.length)]; 

NSRange matchRange = [textCheckingResult rangeAtIndex:1]; 
NSString *match = [xml substringWithRange:matchRange]; 
NSLog(@"Found string '%@'", match); 

NSLog उत्पादन:

Found string '6' 
+0

यदि मैं टैग के बीच "myName" मान प्राप्त करना चाहता हूं तो क्या मुझे नई नियमित अभिव्यक्ति बनाने की आवश्यकता है या मैं इसे वही कर सकता हूं। – Prazi

+1

इसे एक नया regex की आवश्यकता होगी क्योंकि "\ d +" एक या अधिक अंक निर्दिष्ट करता है। एक नाम के लिए आपको "\ S +" (यदि नाम में कोई स्थान नहीं है) की आवश्यकता होगी। एक और सामान्य रेगेक्स है जो किसी भी मामले में काम करना चाहिए: @ " ([^ <] +)", जो कि "<" वर्ण तक कुछ भी कहता है। – zaph

+0

आपकी सभी मदद के लिए धन्यवाद। बहुत सराहना की। .... तो इसका मतलब है कि मुझे दो अलग-अलग पैटर्न की आवश्यकता है 1. ([^ <] +) बैज प्राप्त करने के लिए 2. ([^ <] +) स्क्रीन नाम प्राप्त करने के लिए। – Prazi

1

में तेजी से 3,0

func getMatchingValueFrom(strXML:String, tag:String) -> String { 
    let pattern : String = "<"+tag+">(\\d+)</"+tag+">" 
    let regexOptions = NSRegularExpression.Options.caseInsensitive 

    do { 
     let regex = try NSRegularExpression(pattern: pattern, options: regexOptions) 
     let textCheckingResult : NSTextCheckingResult = regex.firstMatch(in: strXML, options: NSRegularExpression.MatchingOptions(rawValue: UInt(0)), range: NSMakeRange(0, strXML.characters.count))! 
     let matchRange : NSRange = textCheckingResult.rangeAt(1) 
     let match : String = (strXML as NSString).substring(with: matchRange) 
     return match 
    } catch { 
     print(pattern + "<-- not found in string -->" + strXML) 
     return "" 
    } 
} 

पुनश्च यह ऐसा करने के लिए: यह obj- में @ zaph के समाधान की तेज समाधान इसी है सी

संबंधित मुद्दे