:
String text = text.replaceAll("(?<=\\b[0-9]{4})cc\\b", "CC");
(?<=\\b[0-9]{4})- this is a positive lookbehind, which will ensure compliance only if ccfour digits precede (no more than 4, and this rule is applied by the word boundary \\b(this corresponds only to the ends of the word, where the word is defined as a group of characters matching \\w+) .In addition, since lookbehinds are zero-width statements; they are not taken into account in a match.
If the amount of cc can change, then it would be easiest to check only one number:
String text = text.replaceAll("(?<=[0-9])cc\\b", "CC");
Jerry source
share