试图将所有标记大写,并在替换方面遇到麻烦。知道为什么"upperCaseString“方法不起作用吗?
NSError *error = nil;
NSMutableString *stringToCap = [NSMutableString stringWithString:@"<kaboom>stuff</kaboom>"];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(</?[a-zA-Z].*?>)" options:NSRegularExpressionCaseInsensitive error:&error];
NSMutableString *modifiedString = [NSMutableString stringWithString:[regex stringByReplacingMatchesInString:stringToCap options:0 range:NSMakeRange(0, [stringToCap length]) withTemplate:@"$1".uppercaseString]];
NSLog(@"%@", modifiedString);产生:当我期望<kaboom>stuff</kaboom>时<KABOOM>stuff</KABOOM>
发布于 2019-06-27 03:14:41
stringByReplacingMatchesInString:options:range:withTemplate:不是那样工作的,最后一个参数的类型是NSString,您要传递的字符串是表达式@"$1".uppercaseString - which‘s @"$1"的结果。
一种可能的算法(伪码):
for NSTextCheckingResult *match in [regex matchesInString:... options:... range:...] do
extract the substring at match.range from modified string
uppercase it
replace the substring at match.range with uppercased resulthttps://stackoverflow.com/questions/56783045
复制相似问题