使用正则表达式在NSString中查找/替换子字符串

Faz*_* Ya 61 regex objective-c ios ios5

我想使用正则表达式来查找&*;我的字符串中的正则表达式模式的每个实例,并从中删除它,因此返回值是没有任何匹配的原始字符串.也希望使用相同的函数来匹配单词之间的多个空格,而是使用单个空格.找不到这样的功能.

示例输入字符串

NSString *str = @"123 &1245; Ross Test  12";
Run Code Online (Sandbox Code Playgroud)

返回值应该是

123 Ross Test 12
Run Code Online (Sandbox Code Playgroud)

如果有任何匹配此图案"&*或多个空格并替换它@"";

nee*_*vek 158

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);
Run Code Online (Sandbox Code Playgroud)

  • 在ObjC中,您应该使用nil而不是NULL.但NULL也有效,因为它是C. (6认同)

lar*_*rva 12

字符串在String扩展中使用regex替换代码

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
Run Code Online (Sandbox Code Playgroud)

解决

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

或者更多

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

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

使用

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"
Run Code Online (Sandbox Code Playgroud)