如何使用NSRegularExpression的replacementStringForResult:inString:offset:template: [英] How do you use NSRegularExpression's replacementStringForResult:inString:offset:template:

查看:509
本文介绍了如何使用NSRegularExpression的replacementStringForResult:inString:offset:template:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列字符,我想与正则表达式匹配,并根据具体字符串替换它们。

I have a series of characters that I want to match with a regular expression, and replace them with specific strings depending on what they are.

示例:

In =>这是要替换的输入字符串1 2& 3

In => "This is the input string where i want to replace 1 2 & 3"

>这是我要替换ONE TWO&THREE的输入字符串

Out => "This is the input string where i want to replace ONE TWO & THREE"

我当前通过使用空格作为分隔符拆分字符串,并解析每个字符串,增量重建字符串。我觉得这是丑陋,缺乏想象力,有点慢。

I currently do this by splitting the string using spaces as the separator, and parsing each string individually, incrementally rebuilding the string. I feel this is ugly, and lacking imagination, and kinda slow.

根据Apple文档,我应该能够使用 replacementStringForResult: inString:offset:template:方法。

According the Apple documentation I should be able to do this using the replacementStringForResult:inString:offset:template: method. However I can't seem to understand how to use it correctly.

推荐答案

您可以使用 c:循环使用 matchesInString:options:range:,它返回一个匹配数组 NSTextCheckingResult s:

You can use the method within a for in loop using matchesInString:options:range:, which returns an array of matches as NSTextCheckingResults:

NSError* error = NULL;
NSRegularExpression* regex = [NSRegularExpression 
                              regularExpressionWithPattern:@"\\b[1-3]\\b"
                              options:NSRegularExpressionCaseInsensitive
                              error:&error]; 

NSString* yourString = @"This is the input string where i want to replace 1 2 & 3";

NSMutableString* mutableString = [yourString mutableCopy];
NSInteger offset = 0; // keeps track of range changes in the string
                      // due to replacements.
for (NSTextCheckingResult* result in [regex matchesInString:yourString 
                                                    options:0 
                                                      range:NSMakeRange(0, [yourString length])]) {

    NSRange resultRange = [result range];   
    resultRange.location += offset; // resultRange.location is updated 
                                    // based on the offset updated below

    // implement your own replace functionality using
    // replacementStringForResult:inString:offset:template:
    // note that in the template $0 is replaced by the match
    NSString* match = [regex replacementStringForResult:result 
                                               inString:mutableString 
                                                 offset:offset 
                                               template:@"$0"];
    NSString* replacement;
    if ([match isEqualToString:@"1"]) {
        replacement = @"ONE";
    } else if ([match isEqualToString:@"2"]) {
        replacement = @"TWO";
    } else if ([match isEqualToString:@"3"]) {
        replacement = @"THREE";
    }

    // make the replacement
    [mutableString replaceCharactersInRange:resultRange withString:replacement];

    // update the offset based on the replacement
    offset += ([replacement length] - resultRange.length);
}

NSLog(@"mutableString: %@", mutableString); // mutableString: This is the input string where i want to replace ONE TWO & THREE

这篇关于如何使用NSRegularExpression的replacementStringForResult:inString:offset:template:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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