8

我有一个打算移植到 Objective-C 的 C# 项目。根据我对 Obj-C 的了解,看起来有各种各样的正则表达式选项令人困惑,但我看不到任何关于用回调进行替换的方法。

我正在寻找与 C# MatchEvaluator 委托或 PHP 的 preg_replace_callback 等效的东西。我想在 C# 中做的一个例子是 -

// 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 ?'

我怎么能在 Objective-C 中做到这一点?在我的实际情况下,Regex 中同时具有前瞻和后瞻断言,因此任何涉及提前查找字符串然后进行一系列直接字符串替换的替代方法都将不起作用。

4

2 回答 2

7

Foundation 有一个NSRegularExpression类(iOS4 及更高版本),它可能对您有用。从文档:

NSRegularExpression 的基本匹配方法是一个 Block 迭代器方法,它允许客户端提供一个 Block 对象,每次正则表达式匹配目标字符串的一部分时都会调用该对象。还有其他方便的方法可以将所有匹配项作为数组、匹配项总数、第一个匹配项和第一个匹配项的范围返回。

例如:

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?
于 2011-01-22T07:55:04.667 回答
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]];

现在似乎可以工作(可能需要更多测试)

于 2012-11-01T14:56:42.730 回答