我有一个C#项目,我打算移植到Objective-C。根据我对Obj-C的理解,它看起来有一个令人困惑的各种Regex选项,但我看不到有关使用回调替换的方法。
我正在寻找的东西相当于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中这样做?在我的实际情况中,正则表达式中既有前瞻性和后瞻性断言,所以任何涉及提前查找字符串然后进行一系列直接字符串替换的替代方案都将无法工作。
答案 0 :(得分:7)
Foundation有一个NSRegularExpression类(iOS4及更高版本),可能对您有用。来自文档:
基本匹配方法 NSRegularExpression是一个块 允许客户端的迭代器方法 提供一个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?
答案 1 :(得分: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]];
现在似乎工作(可能需要更多测试)