是否有一个 Objective-c 正则表达式替换为回调/C# MatchEvaluator 等效项? [英] Is there an Objective-c regex replace with callback/C# MatchEvaluator equivalent?

查看:19
本文介绍了是否有一个 Objective-c 正则表达式替换为回调/C# MatchEvaluator 等效项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C# 项目,我打算移植到 Objective-C.从我对 Obj-C 的理解来看,Regex 选项的种类似乎令人困惑,但我看不到任何关于用回调进行替换的方法.

I have a C# project I'm intending to port to Objective-C. From what I understand about Obj-C, it looks like there's a confusing variety of Regex options but I can't see anything about a way of doing a replace with callback.

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

I'm looking for something that is the equivalent of the C# MatchEvaluator delegate or PHP's preg_replace_callback. An example of what I want to do in C# is -

// 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 中同时包含前瞻和后视断言,因此任何涉及提前查找字符串然后进行一系列直接字符串替换的替代方法都将无法正常工作.

How could I do this in Objective-C ? In my actual situation the Regex has both lookahead and lookbehind assertions in it though, so any alternative involving finding the strings in advance and then doing a series of straight string replaces won't work unfortunately.

推荐答案

Foundation 有一个 NSRegularExpression 类(iOS4 及更高版本),可能对您有用.来自文档:

Foundation has a NSRegularExpression class (iOS4 and later), which may be useful to you. From the docs:

基本匹配方法NSRegularExpression 是一个块允许客户端的迭代器方法提供一个 Block 对象,它将是每次定期调用表达式匹配的一部分目标字符串.还有额外的返回所有的便捷方法匹配作为一个数组,总数匹配次数,第一次匹配,以及第一个匹配的范围.

The fundamental matching method for NSRegularExpression is a Block iterator method that allows clients to supply a Block object which will be invoked each time the regular expression matches a portion of the target string. There are additional convenience methods for returning all the matches as an array, the total number of matches, the first match, and the range of the first match.

例如:

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?

这篇关于是否有一个 Objective-c 正则表达式替换为回调/C# MatchEvaluator 等效项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆