ios Use regular expression to find/replace substring in NSString
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9661690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use regular expression to find/replace substring in NSString
提问by Faz Ya
I would like to use regular expression to find every instances of a regular expression pattern I.e. &*;
in my string and remove that from so the return value is the original string without any of the matches. Also would like to use the same function to match multiple spaces between words and have a single space instead. Could not find such a function.
I would like to use regular expression to find every instances of a regular expression pattern I.e. &*;
in my string and remove that from so the return value is the original string without any of the matches. Also would like to use the same function to match multiple spaces between words and have a single space instead. Could not find such a function.
Sample input string
Sample input string
NSString *str = @"123 &1245; Ross Test 12";
Return value should be
Return value should be
123 Ross Test 12
If anything matching this pattern "&*
or multiple white spaces and replaces it with @"";
If anything matching this pattern "&*
or multiple white spaces and replaces it with @"";
回答by neevek
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);
回答by larva
String replacing code using regex in String extension
String replacing code using regex in String extension
Objective-C
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
resolve
resolve
NSString *string = @"123 &1245; Ross Test 12";
// remove all matches string
NSString *result = [string replacingWithPattern:@"&[\d]+?;" withTemplate:@"" error:nil];
// result = "123 Ross Test 12"
or more
or more
NSString *string = @"123 + 456";
// swap number
NSString *result = [string replacingWithPattern:@"([\d]+)[ \+]+([\d]+)" withTemplate:@" + " error:nil];
// result = 456 + 123
Swift2
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
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)
}
}
use
use
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"