Utiliser l'expression régulière pour searchr / replace la sous-string dans NSSsortingng

Je voudrais utiliser l'expression régulière pour find toutes les occurrences d'un motif d'expression régulière Ie &*; dans ma string et supprimer cela de sorte que la valeur de return est la string d'origine sans aucune des correspondances. Aussi voudrait utiliser la même fonction pour faire correspondre plusieurs espaces entre les mots et avoir un seul espace à la place. Impossible de find une telle fonction.

Exemple de string d'input

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

La valeur de return devrait être

 123 Ross Test 12 

Si quelque chose correspond à ce model "&* ou plusieurs espaces blancs et le remplace par @"";

 NSSsortingng *ssortingng = @"123 &1245; Ross Test 12"; NSError *error = nil; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"&[^;]*;" options:NSRegularExpressionCaseInsensitive error:&error]; NSSsortingng *modifiedSsortingng = [regex ssortingngByReplacingMatchesInSsortingng:ssortingng options:0 range:NSMakeRange(0, [ssortingng length]) withTemplate:@""]; NSLog(@"%@", modifiedSsortingng); 

Chaîne remplaçant le code en utilisant regex dans l'extension Ssortingng

Objectif c

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

résoudre

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

ou plus

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

Swift2

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

Swift3

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

utilisation

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