This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Can anyone tell me how to get the replacement text for each matcher capturing group?
example: I want to replace "WARM tamales" with "WARMER tamales", using the search pattern "(HOT|COLD|WARM|)" and the replacement pattern "$1ER". This works fine, but I also want to be able to discover what was the final replacement text used for capturing group 1 (eg. in this example, it would be the text WARMER, which replaced the original text WARM).
Does this make sense? Can it be done?
edit: oh yeah, here's some code...
edit: Fixed the comments in the code (thanks Rob!)
The replaceAll() method is merely a convenience method that uses the lower level find(), appendReplacement(), and appendTail() methods. An option is to not use replaceAll(), but use the lower level methods directly, to do the replacement, and to save a copy of the replacement.
The javadoc for the append methods has an example of how the three methods work together...