2012-03-12 27 views
55

Vorrei utilizzare l'espressione regolare per trovare tutte le istanze di un modello di espressione regolare I.e. &*; nella mia stringa e rimuovere quello da così il valore di ritorno è la stringa originale senza alcuna delle corrispondenze. Vorrebbe anche usare la stessa funzione per abbinare più spazi tra le parole e avere invece uno spazio singolo. Impossibile trovare una funzione simile.Utilizzare l'espressione regolare per trovare/sostituire la sottostringa in NSString

Esempio stringa di input

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

valore di ritorno dovrebbe essere

123 Ross Test 12 

Semmai corrispondenza questo pattern "&* o più spazi bianchi e lo sostituisce con il codice @"";

risposta

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

In objC si dovrebbe usare nil invece di NULL. Ma anche NULL funziona, dato che è C. – Daniel

6

String sostituzione usando regex nell'estensione di stringa

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 

determinazione

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

o più

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

uso

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" 
Problemi correlati