2012-03-12 11 views
55

Chciałbym użyć wyrażenia regularnego, aby znaleźć wszystkie wystąpienia wzorca regularnego wyrażenia, np. &*; w moim ciągu znaków i usuń go, aby wartość zwracana była oryginalnym łańcuchem bez żadnych dopasowań. Również chciałoby użyć tej samej funkcji, aby dopasować wiele spacji między słowami i zamiast tego mieć pojedynczą spację. Nie można znaleźć takiej funkcji.Użyj wyrażenia regularnego do znalezienia/zamiany podciągu w NSString

Przykładowy ciąg wejściowy

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

Zwracana wartość powinna być

123 Ross Test 12 

Jeśli coś dopasowanie ten wzór "&* lub kilka spacji i zastępuje go @"";

Odpowiedz

150
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); 
+6

W ObjC należy użyć nil zamiast NULL. Ale NULL również działa, ponieważ jest to C. – Daniel

6

String zastępując kod użycie regex w rozszerzeniu String

Cel 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 

rozwiązać

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

lub więcej

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) 
    } 
} 

użycie

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" 
Powiązane problemy