gpt4 book ai didi

objective-c - 是否有一个 Objective-c 正则表达式替换为回调/C# MatchEvaluator 等效项?

转载 作者:太空狗 更新时间:2023-10-30 03:32:24 24 4
gpt4 key购买 nike

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

我正在寻找与 C# MatchEvaluator 委托(delegate)或 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 中同时包含先行断言和后行断言,因此任何涉及提前查找字符串然后进行一系列直接字符串替换的替代方法都不会奏效。

最佳答案

基金会有一个 NSRegularExpression类(iOS4 及更高版本),这可能对您有用。来自文档:

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 - 是否有一个 Objective-c 正则表达式替换为回调/C# MatchEvaluator 等效项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3957092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com