November 2005
Intermediate to advanced
304 pages
6h 14m
English
In the previous section, we created regular expressions for matching patterns in a String and for retrieving data from a subpattern group. With a regex, we can also substitute new values for the matching patterns. One way to do this is with the replaceAll method
of the Matcher class. It returns a String where all matching substrings are replaced with a given String parameter. To illustrate, let's find all occurrences of the word repetition within a document and replace them with the word duplication:
String data = getStringData(); Pattern repPattern = Pattern.compile("(\\s)(repetition)([\\s;\\.,])"); ...Read now
Unlock full access