Java Reference
In-Depth Information
How It Works
The regular expression here defines four capturing groups:
Group 0: The whole expression.
Group 1: The subexpression " (\\d+(\\.\\d*)?) "
Group 2: The subexpression " (\\.\\d*) "
Group 3: The subexpression " (\\.\\d+) "
After each successful call of the find() method for the Matcher object, m , we output the text captured
by each group in turn by passing the index value for the group to the group() method. Note that
because we want to output group 0 as well as the other groups, we start the loop index from 0 and allow
it to equal the value returned by groupCount() so as to index over all the groups.
You can see from the output that group 1 corresponds to numbers beginning with a digit and group 3
corresponds to numbers starting with a decimal point, so either one or the other of these is always
null . Group 2 corresponds to the sub-pattern within group 1 that matches the fractional part of a
number that begins with a digit, so the text for this can only be non- null when the text for group 1 is
non- null and the number has a decimal point.
Juggling Captured Text
Since we can get access to the text corresponding to each capturing group in a regular expression, we
can move them around. The appendReplacement() method has special provision for recognizing
references to capturing groups in the replacement text string. If $n , where n is an integer, appears in the
replacement string, it will be interpreted as the text corresponding to group n . You can therefore
replace the text matched to a complete pattern by any sequence of your choosing of the sub sequences
corresponding to the capturing groups in the pattern. That's hard to describe in words, so let's
demonstrate it with an example.
Try It Out - Rearranging Captured Group Text
I'm sure you remember that the Math.pow() method requires two arguments; the second argument is
the power to which the first argument must be raised. Thus to calculate 16 3 you can write:
double result = Math.pow(16.0, 3.0);
Let's suppose we have written a Java program where we have mistakenly switched the two arguments so
in trying to compute 16 3 we have written:
double result = Math.pow(3.0, 16.0);
Of course, this computes 3 16 , which is not quite the same thing. Let's suppose further that this sort of
error is strewn throughout the source code and in every case we have the arguments the wrong way
round. We would need a month of Sundays to go through manually and switch the argument values so
let's see if regular expressions can rescue the situation.
What we need to do is find each occurrence of Math.pow() and switch the arguments around. The
intention here is to understand how we can switch things around so we will keep it simple and assume
that the argument values to Math.pow() are always a numerical value or a variable name.
Search WWH ::




Custom Search