How do you use NSRegularExpression's replacementStringForResult:inString:offset:template:

前端 未结 6 935
清酒与你
清酒与你 2020-12-31 06:30

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.

Example:

In

6条回答
  •  遥遥无期
    2020-12-31 07:12

    I needed a more generic solution for the same problem so I refined Dano's answer to a method like this, with a sample usage explained below:

    - (NSMutableString *)replaceSubstringsInString:(NSString*)string
                                        usingRegex:(NSString*)searchRegex
                                  withReplacements:(NSDictionary*)replacements {
    
        NSMutableString *newString = [string mutableCopy];
        __block NSInteger offset = 0;
    
        NSError *error = NULL;
    
        NSRegularExpression *regex = [NSRegularExpression
                                      regularExpressionWithPattern:searchRegex
                                      options:0
                                      error:&error];
    
        [regex enumerateMatchesInString:string
                                options:0
                                  range:NSMakeRange(0, [string length])
                             usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
    
                                 NSRange resultRange = [match range];
                                 resultRange.location += offset;
    
                                 NSString *substring = [string substringWithRange:match.range];
    
                                 __block NSString* replacement;
    
                                 [replacements enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    
                                     if ([key isEqualToString:substring]) {
                                         replacement = obj;
                                     }
    
                                 }];
    
                                 [newString replaceCharactersInRange:resultRange withString:replacement];
    
                                 offset += ([replacement length] - resultRange.length);
                             }];
    
        return newString;
    }
    

    Usage:

    NSString *string = @"This is the input string where i want to replace 1 2 & 3";
    NSString *searchRegex = @"\\b[1-3]\\b";
    NSDictionary *replacements = @{@"1":@"ONE",@"2":@"TWO",@"3":@"THREE"};
    
    NSMutableString *result = [self replaceSubstringsInString:string
                                                   usingRegex:searchRegex
                                             withReplacements:replacements];
    

    Explanation: You just have to pass in the string to search for the matching substrings, along with the desired regexSearch pattern and a replacements dictionary containing the string pairs with the key being the substring to be replaced and the object with the desired replacement string.

    Output:

     //   This is the input string where i want to replace ONE TWO & THREE
    

提交回复
热议问题