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

查看:336
本文介绍了是否有一个Objective-c regex替换回调/ 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中这样做?在我的实际情况下,正则表达式有它的lookahead和lookbehind断言,所以任何替代涉及到提前找到字符串,然后做一系列直字符串替换不会工作不幸。

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
迭代器方法, b $ b提供一个Block对象,它将在每次正则
表达式匹配
目标字符串的一部分时调用
。还有额外的
方便方法用于返回所有
匹配作为数组,总
匹配数,第一个匹配,
和第一个匹配的范围。 p>

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 regex替换回调/ C#MatchEvaluator等效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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