2010-10-18 7 views
8

मेरे पास एक सी # प्रोजेक्ट है जिसका उद्देश्य मैं उद्देश्य-सी पर बंदरगाह का इरादा रख रहा हूं। ओबज-सी के बारे में जो कुछ मैं समझता हूं, उससे ऐसा लगता है कि रेगेक्स विकल्पों की एक भ्रमित विविधता है लेकिन मुझे कॉलबैक के साथ प्रतिस्थापन करने के तरीके के बारे में कुछ भी नहीं दिखाई दे रहा है।क्या कोई उद्देश्य-सी regex कॉलबैक/सी # MatchEvaluator समकक्ष के साथ प्रतिस्थापित है?

मैं कुछ ऐसा ढूंढ रहा हूं जो C# MatchEvaluator प्रतिनिधि या PHP के preg_replace_callback के बराबर है। सी # में मैं क्या करना चाहता हूं इसका एक उदाहरण है -

// change input so each word is followed a number showing how many letters it has 

string inputString = "Hello, how are you today ?"; 
Regex theRegex = new Regex(@"\w+"); 

string outputString = theRegex.Replace(inputString, delegate (Match thisMatch){ 
    return thisMatch.Value + thisMatch.Value.Length; 
}); 

// outputString is now 'Hello5, how3 are3 you3 today5 ?' 

मैं उद्देश्य-सी में यह कैसे कर सकता हूं? मेरी वास्तविक स्थिति में रेगेक्स दोनों में लुकहेड है और इसके बावजूद दावा है, इसलिए किसी भी विकल्प को स्ट्रिंग्स को अग्रिम में ढूंढना और फिर सीधे स्ट्रिंग प्रतिस्थापन की श्रृंखला करना दुर्भाग्यवश काम नहीं करेगा।

उत्तर

7

फाउंडेशन में NSRegularExpression वर्ग (आईओएस 4 और बाद में) है, जो आपके लिए उपयोगी हो सकता है। डॉक्स से:

के लिए मौलिक मिलान विधि NSRegularExpression एक ब्लॉक इटरेटर विधि है कि लिए ग्राहकों की अनुमति देता है एक ब्लॉक वस्तु की आपूर्ति कर रहा है जो हो जाएगा हर बार नियमित अभिव्यक्ति लक्ष्य के एक हिस्से से मेल खाता लागू स्ट्रिंग। अतिरिक्त सभी मैचों को सरणी के रूप में वापस करने के लिए सुविधा विधियों, कुल मैचों की संख्या, पहला मैच, और पहले मैच की सीमा को वापस करने के लिए सुविधा विधियां हैं।

उदाहरण के लिए:

NSString *input = @"Hello, how are you today?"; 

// make a copy of the input string. we are going to edit this one as we iterate 
NSMutableString *output = [NSMutableString stringWithString:input]; 

NSError *error = NULL; 
NSRegularExpression *regex = [NSRegularExpression 
           regularExpressionWithPattern:@"\\w+" 
                options:NSRegularExpressionCaseInsensitive 
                 error:&error]; 

// keep track of how many additional characters we've added (1 per iteration) 
__block NSUInteger count = 0; 

[regex enumerateMatchesInString:input 
         options:0 
          range:NSMakeRange(0, [input length]) 
        usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){ 

    // Note that Blocks in Objective C are basically closures 
    // so they will keep a constant copy of variables that were in scope 
    // when the block was declared 
    // unless you prefix the variable with the __block qualifier 

    // match.range is a C struct 
    // match.range.location is the character offset of the match 
    // match.range.length is the length of the match   

    NSString *matchedword = [input substringWithRange:match.range]; 

    // the matched word with the length appended 
    NSString *new = [matchedword stringByAppendingFormat:@"%d", [matchedword length]]; 

    // every iteration, the output string is getting longer 
    // so we need to adjust the range that we are editing 
    NSRange newrange = NSMakeRange(match.range.location+count, match.range.length); 
    [output replaceCharactersInRange:newrange withString:new]; 

    count++; 
}]; 
NSLog(@"%@", output); //output: Hello5, how3 are3 you3 today5? 
3

मैं यह थोड़ा और अधिक लचीला बनाने के लिए atshum के कोड को संशोधित:

__block int prevEndPosition = 0; 
[regex enumerateMatchesInString:text 
         options:0 
          range:NSMakeRange(0, [text length]) 
        usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) 
{ 
    NSRange r = {.location = prevEndPosition, .length = match.range.location - prevEndPosition}; 

    // Copy everything without modification between previous replacement and new one 
    [output appendString:[text substringWithRange:r]]; 
    // Append string to be replaced 
    [output appendString:@"REPLACED"]; 

    prevEndPosition = match.range.location + match.range.length; 
}]; 

// Finalize string end 
NSRange r = {.location = prevEndPosition, .length = [text length] - prevEndPosition}; 
[output appendString:[text substringWithRange:r]]; 

अब के लिए काम करने के लिए लगता है (शायद थोड़ा और परीक्षण की जरूरत है)

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