我想用enumerateMatchInString
找到每個縮寫a 調用我的方法ConvertAbbreviation:(NSString *)abbr;
來隱藏該縮寫並返回完整的字符串。使用NSRegularExpression替換每個匹配的方法結果
有人可以幫助我?我在嘗試下面的代碼時遇到了異常情況。 如果這是不對的,請給我一個如何修改塊內原始文本的例子。
- (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;
}
你可能不希望匹配的尾隨空白。使用積極的向前看:'[a-zA-Z。] + \\。(?= \\ s | \\ n | $)'。它會匹配它,但不包括它在實際的匹配結果中。 – Regexident 2012-07-10 12:33:19