如果只需要转换
cc为大写字母,并且固定了大小写,则可以将匹配替换为
CC。
Java中没有针对此的单线通用解决方案。您必须使用
Matcher#appendReplacement()和完成此操作
Matcher#appendTail():
String str = "Refurbished Engine for 2000cc Vehicles";Pattern pattern = Pattern.compile("\d{4}cc");Matcher matcher = pattern.matcher(str);StringBuffer result = new StringBuffer();while (matcher.find()) { matcher.appendReplacement(result, matcher.group().toUpperCase());}matcher.appendTail(result);System.out.println(result.toString());


