So I have a string like
Refurbished Engine for 2000cc Vehicles
I would like to turn this into
Refurbish
You could perhaps do:
String text = text.replaceAll("(?<=\\b[0-9]{4})cc\\b", "CC");
(?<=\\b[0-9]{4})
is a positive lookbehind that will ensure a match only if cc
is preceded by 4 digits (no more than 4 and this rule is enforced by the word boundary \\b
(this matches only at the ends of a word, where a word is defined as a group of characters matching \\w+
). Also, since lookbehinds are zero-width assertions, they don't count in the match.
If the number of cc's can vary, then it might be easiest checking only one number:
String text = text.replaceAll("(?<=[0-9])cc\\b", "CC");
You can try with
text = text.replaceAll("(\\d{4})cc", "$1CC");
// ↓ ↑
// +→→→→→→→→→→+
Trick is to place number in group (via parenthesis) and later use match from this group in replacement part (via $x
where x
is group number).
You can surround that regex with word boundaries "\\b"
if you want to make sure that matched text is not part of some other word. You can also use look-adound mechanisms to ensure that there are no alphanumeric characters before and/or after matched text.
One way is to trap the numeric part as a group with () and then use a backreference to that group in the substitution:
This is tested:
public static void main(String [] args) {
String s = "1000cc abc 9999cc";
String t = s.replaceAll("(\\d{4})cc", "$1CC");
System.err.println(t);
}
If you just have to convert cc
to uppercase, and if it is fixed, then you can just replace the match with CC
.
There is no one-liner generic solution for this in Java. You have to do this with Matcher#appendReplacement()
and 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());