I’d like to use enumerateMatchInString to find every abbreviation a
call my method ConvertAbbreviation:(NSString *)abbr; to covert that abbreviation and return the full string.
Can someone help me with this? I am getting exceptions when I try the code below.
If this is wrong can someone please give me an example of how to modify the original text within the block.
- (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;
}
You should try a backwards search, otherwise once you modify the text the first time your text ranges are no longer valid.
So, enumerate over all matches like this (you can use this trick to go backwards):