Faz*_* Ya 1 regex objective-c nsregularexpression
我想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)
bor*_*den 11
您应该尝试向后搜索,否则在第一次文本范围不再有效时修改文本.
所以,列举这样的所有匹配(你可以使用这个技巧倒退):
NSArray *results = [matchAnyPotentialAbv matchesInString:textBlock options:kNilOptions range:NSMakeRange(0, textBlock.length)];
for(NSTextCheckingResult *result in [results reverseObjectEnumerator])
{
//Replace
}
Run Code Online (Sandbox Code Playgroud)