You can use Matcher.appendReplacement()/appendTail() to build very flexible search-and-replace features.
The example in the JavaDoc looks like this:
Pattern p = Pattern.compile("cat");
Matcher m = p.matcher("one cat two cats in the yard");
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, "dog");
}
m.appendTail(sb);
System.out.println(sb.toString());
Now, inside that while
loop you can decide yourself what the replacement text is and base that information on the actual content being matched.
For example you could use the pattern (,|cat|football)
to match ,
, cat
or football
and decide on the actual replacement based on the actual match inside the loop.
You can make build even more flexible things this way, such as replacing all decimal numbers with hex numbers or similar operations.
It's not as short and simple as your code, but you can build short and simple methods with it.