我想使用正则表达式来查找正则表达式模式的每个实例,即&*;
在我的字符串中并从中删除它,因此返回值是没有任何匹配项的原始字符串.也想使用相同的功能来匹配单词之间的多个空格,并改为使用单个空格.找不到这样的功能.
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.
示例输入字符串
NSString *str = @"123 &1245; Ross Test 12";
返回值应该是
123 Ross Test 12
如果有任何匹配此模式 "&*
或多个空格并将其替换为 @"";
If anything matching this pattern "&*
or multiple white spaces and replaces it with @"";
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);
这篇关于使用正则表达式查找/替换 NSString 中的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!