我想使用正则表达式来查找&*;我的字符串中的正则表达式模式的每个实例,并从中删除它,因此返回值是没有任何匹配的原始字符串.也希望使用相同的函数来匹配单词之间的多个空格,而是使用单个空格.找不到这样的功能.
示例输入字符串
NSString *str = @"123 &1245; Ross Test 12";
Run Code Online (Sandbox Code Playgroud)
返回值应该是
123 Ross Test 12
Run Code Online (Sandbox Code Playgroud)
如果有任何匹配此图案"&*或多个空格并替换它@"";
我想enumerateMatchInString用来查找每个缩写的一个调用我的方法ConvertAbbreviation:(NSString *)abbr;来隐藏该缩写并返回完整的字符串.
有人可以帮我弄这个吗?我在尝试下面的代码时遇到异常.如果这是错的,有人可以给我一个如何修改块内原始文本的例子.
- (NSString *) convertAllAbbreviationsToFullWords:(NSString *) textBlock {
NSRegularExpression *matchAnyPotentialAbv = [NSRegularExpression regularExpressionWithPattern:@"[a-zA-Z\\.]+\\.\\s" options:0 error:nil];
[matchAnyPotentialAbv
enumerateMatchesInString:textBlock
options:0
range:NSMakeRange(0, [textBlock length])
usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {
/* on each match, I would like to call convert(match) and
replace the result with the existing match in my textBlock */
if (++count >= 200) *stop = YES;
}
];
return textBlock;
}
Run Code Online (Sandbox Code Playgroud)