1

我正在寻找一种有效的方法来用替换词“链接”替换 NSString(或 NSMutableString)中的 URL,例如......

@"This is a sample with **http://bitbanter.com** within the string and heres another **http://spikyorange.co.uk** for luck"

我希望这成为...

@"This is a sample with **'link'** within the string and heres another **'link'** for luck"

理想情况下,我希望这是一种接受正则表达式的方法,但是,这需要在 iPhone 上工作,最好没有任何库,或者,如果库很小,我可能会被说服。

其他方便的功能,替换@"OMG"@"Oh my God",但不是当它是单词的一部分时,即@"DOOMGAME"不应该被触及。

任何建议表示赞赏。

问候,罗布。

4

2 回答 2

3

这实际上很有趣,希望解决方案是您正在寻找的解决方案。这足够灵活,不仅可以满足链接的需求,还可以满足您可能希望使用某些条件将单词替换为另一个单词的其他模式:

我已经评论了大部分代码,所以它应该很容易解释。如果没有,请随时发表评论,我会尽力提供帮助:

- (NSString*)replacePattern:(NSString*)pattern withReplacement:(NSString*)replacement forString:(NSString*)string usingCharacterSet:(NSCharacterSet*)characterSetOrNil
{
    // Check if a NSCharacterSet has been provided, otherwise use our "default" one
    if (!characterSetOrNil)
    characterSetOrNil = [NSCharacterSet characterSetWithCharactersInString:@" !?,()]"];

    // Create a mutable copy of the string supplied, setup all the default variables we'll need to use
    NSMutableString *mutableString = [[[NSMutableString alloc] initWithString:string] autorelease];
    NSString *beforePatternString = nil;
    NSRange outputrange = NSMakeRange(0, 0);

    // Check if the string contains the "pattern" you're looking for, otherwise simply return it.
    NSRange containsPattern = [mutableString rangeOfString:pattern];
    while (containsPattern.location != NSNotFound)
    // Found the pattern, let's run with the changes
    {
        // Firstly, we grab the full string range
        NSRange stringrange = NSMakeRange(0, [mutableString length]);
        NSScanner *scanner = [[NSScanner alloc] initWithString:mutableString];

        // Now we use NSScanner to scan UP TO the pattern provided
        [scanner scanUpToString:pattern intoString:&beforePatternString];

        // Check for nil here otherwise you will crash - you will get nil if the pattern is at the very beginning of the string
        // outputrange represents the range of the string right BEFORE your pattern
        // We need this to know where to start searching for our characterset (i.e. end of output range = beginning of our pattern)
        if (beforePatternString != nil)
            outputrange = [mutableString rangeOfString:beforePatternString];

        // Search for any of the character sets supplied to know where to stop.
        // i.e. for a URL you'd be looking at non-URL friendly characters, including spaces (this may need a bit more research for an exhaustive list)
        NSRange characterAfterPatternRange = [mutableString rangeOfCharacterFromSet:characterSetOrNil options:NSLiteralSearch range:NSMakeRange(outputrange.length, stringrange.length-outputrange.length)];

        // Check if the link is not at the very end of the string, in which case there will be no characters AFTER it so set the NSRage location to the end of the string (== it's length)
        if (characterAfterPatternRange.location == NSNotFound)
            characterAfterPatternRange.location = [mutableString length];

        // Assign the pattern's start position and length, and then replace it with the pattern
        NSInteger patternStartPosition = outputrange.length;
        NSInteger patternLength = characterAfterPatternRange.location - outputrange.length;
        [mutableString replaceCharactersInRange:NSMakeRange(patternStartPosition, patternLength) withString:replacement];
        [scanner release];

        // Reset containsPattern for new mutablestring and let the loop continue
        containsPattern = [mutableString rangeOfString:pattern];
    }
    return [[mutableString copy] autorelease];
}

并以您的问题为例,您可以这样称呼它:

NSString *firstString = @"OMG!!!! this is the best convenience method ever, seriously! It even works with URLs like http://www.stackoverflow.com";
NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@" !?,()]"];
NSString *returnedFirstString = [self replacePattern:@"OMG" withReplacement:@"Oh my God" forString:firstString usingCharacterSet:characterSet];
NSString *returnedSecondString = [self replacePattern:@"http://" withReplacement:@"LINK" forString:returnedFirstString usingCharacterSet:characterSet];
NSLog (@"Original string = %@\nFirst returned string = %@\nSecond returned string = %@", firstString, returnedFirstString, returnedSecondString);

我希望它有帮助!干杯,罗格

于 2011-03-10T02:05:24.073 回答
0

从 iOS 4 开始,NSRegularExpression可用。除此之外,您可以通过块枚举字符串中的所有匹配项,允许您对每个匹配项做任何您想做的事情,或者让正则表达式直接为您执行某种替换。

直接字符串替换(如 'OMG' -> 'Oh my God')可以直接由 NSString 执行,使用-stringByReplacingOccurencesOfString:withString:replaceOccurrencesOfString:withString:options:range:如果您的字符串是可变的。

于 2011-03-09T21:17:36.863 回答