正则表达式在iOS中替换

Ted*_*Ted 7 regex ios nsregularexpression

我将需要替换一个干净的字符串脏字符串:

-(void)setTheFilter:(NSString*)filter
{
    [filter retain];
    [_theFilter release];

    <PSEUDO CODE>
    tmp = preg_replace(@"/[0-9]/",@"",filter);
    <~PSEUDO CODE>

    _theFilter = tmp;
}
Run Code Online (Sandbox Code Playgroud)

这应该消除所有数字,filter以便:

@"Import6652"
会产生@"导入"

我怎么能在iOS中做到这一点?

谢谢!

msk*_*msk 22

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:
                              @"([0-9]+)" options:0 error:nil];

[regex replaceMatchesInString:str options:0 range:NSMakeRange(0, [str length]) withTemplate:@""];
Run Code Online (Sandbox Code Playgroud)

迅速

do {
    let regex = try NSRegularExpression(pattern: "([0-9]+)", options: NSRegularExpressionOptions.CaseInsensitive)
    regex.replaceMatchesInString(str, options: NSMatchingOptions.ReportProgress, range: NSRange(0,str.characters.count), withTemplate: "")
} catch {}
Run Code Online (Sandbox Code Playgroud)


bor*_*den 1

请参阅NSRegularExpression的文档

特别是标题为“使用正则表达式替换字符串”的部分