I'm looking for a very simple way of getting the equivalent of something like the following JavaScript code. That is, for each match I would like to call a certain transformation function and use the result as the replacement value.
var res = "Hello World!".replace(/S+/, function (word) {
// Since this function represents a transformation,
// replacing literal strings (as with replaceAll) are not a viable solution.
return "" + word.length;
})
// res => "5 6"
Only .. in Java. And, preferably as a "single method" or "template" that can be reused.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…