2012-03-12 34 views
55

Tôi muốn sử dụng cụm từ thông dụng để tìm mọi trường hợp của mẫu biểu thức chính quy I.e. &*; trong chuỗi của tôi và xóa nó khỏi để giá trị trả về là chuỗi gốc mà không có bất kỳ kết quả phù hợp nào. Ngoài ra, cũng muốn sử dụng cùng một hàm để đối sánh nhiều dấu cách giữa các từ và có một dấu cách. Không thể tìm thấy chức năng như vậy.Sử dụng cụm từ thông dụng để tìm/thay thế chuỗi con trong NSString

mẫu chuỗi đầu vào

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

giá trị Return nên

123 Ross Test 12 

Nếu bất cứ điều gì phù hợp với mô hình này "&* hoặc nhiều không gian màu trắng và thay thế nó bằng @"";

Trả lời

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

Trong ObjC bạn nên sử dụng con số không thay vì NULL. Nhưng NULL cũng hoạt động, vì nó là C. – Daniel

6

Chuỗi thay thế mã sử dụng regex trong tiện ích mở rộng chuỗi

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 

quyết

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

hoặc nhiều

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

sử dụng

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" 
Các vấn đề liên quan