【问题标题】:Use regular expression to find/replace substring in NSString使用正则表达式查找/替换 NSString 中的子字符串
【发布时间】:2012-03-28 12:50:27
【问题描述】:

我想使用正则表达式来查找正则表达式模式的每个实例,即&*; 在我的字符串中并从中删除,因此返回值是没有任何匹配项的原始字符串。还想使用相同的功能来匹配单词之间的多个空格,并改为使用单个空格。找不到这样的功能。

示例输入字符串

NSString *str = @"123 &1245; Ross Test  12";

返回值应该是

123 Ross Test 12

如果有任何匹配此模式 "&* 或多个空格并将其替换为 @"";

【问题讨论】:

    标签: objective-c ios regex ios5


    【解决方案1】:
    NSString *string = @"123 &1245; Ross Test 12";
    NSError *error = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"&[^;]*;" options:NSRegularExpressionCaseInsensitive error:&error];
    NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@""];
    NSLog(@"%@", modifiedString);
    

    【讨论】:

    • 在 ObjC 中你应该使用 nil 而不是 NULL。但是 NULL 也可以,因为它是 C。
    【解决方案2】:

    在字符串扩展中使用正则表达式替换字符串

    Objective-C

    @implementation NSString(RegularExpression)
    
    - (NSString *)replacingWithPattern:(NSString *)pattern withTemplate:(NSString *)withTemplate error:(NSError **)error {
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
                                                                               options:NSRegularExpressionCaseInsensitive
                                                                                 error:error];
        return [regex stringByReplacingMatchesInString:self
                                               options:0
                                                 range:NSMakeRange(0, self.length)
                                          withTemplate:withTemplate];
    }
    
    @end
    

    解决

    NSString *string = @"123 &1245; Ross Test  12";
    // remove all matches string
    NSString *result = [string replacingWithPattern:@"&[\\d]+?;" withTemplate:@"" error:nil];
    // result = "123  Ross Test  12"
    

    或更多

    NSString *string = @"123 +   456";
    // swap number
    NSString *result = [string replacingWithPattern:@"([\\d]+)[ \\+]+([\\d]+)" withTemplate:@"$2 + $1" error:nil];
    // result = 456 + 123
    

    Swift2

    extension String {
        func replacing(pattern: String, withTemplate: String) throws -> String {
            let regex = try NSRegularExpression(pattern: pattern, options: .CaseInsensitive)
            return regex.stringByReplacingMatchesInString(self, options: [], range: NSRange(0..<self.utf16.count), withTemplate: withTemplate)
        }
    }
    

    Swift3

    extension String {
        func replacing(pattern: String, withTemplate: String) throws -> String {
            let regex = try RegularExpression(pattern: pattern, options: .caseInsensitive)
            return regex.stringByReplacingMatches(in: self, options: [], range: NSRange(0..<self.utf16.count), withTemplate: withTemplate)
        }
    }
    

    使用

    var string = "1!I 2\"want 3#to 4$remove 5%all 6&digit and a char right after 7'from 8(string"
    do {
        let result = try string.replacing("[\\d]+.", withTemplate: "")
    } catch {
        // error
    }
    // result = "I want to remove all digit and a char right after from string"
    

    【讨论】:

    • 最好将函数名重命名为- (NSString *)stringByReplacingWithPattern:(NSString *)pattern withTemplate:(NSString *)withTemplate error:(NSError **)error
    • 最好删除所有带有
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    • 2012-07-11
    相关资源
    最近更新 更多