Java Reference
In-Depth Information
begins with a digit, so the text for this can be non- null only when the text for group 1 is non- null and
the number has a decimal point.
Juggling Captured Text
Because you can get access to the text corresponding to each capturing group, you can move such blocks of
text around. The appendReplacement() method has special provision for recognizing references to captur-
ing groups in the replacement text string. If $n , where n is an integer, appears in the replacement string, it
is 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 subsequences 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 a weak programmer on your team has written a Java program in which the two arguments have mis-
takenly been switched, so in trying to compute 16 3 the programmer has 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 the arguments are the wrong way round. You 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 you need to do is find each occurrence of Math.pow() and switch the arguments around. The intention here
is to understand how you can switch things around, so I'll keep it simple and assume that the argument values to
Math.pow() are always a numerical value or a variable name.
The key to the whole problem is to devise a regular expression with capturing groups for the bits you want to switch
— the two arguments. Be warned: This is going to get a little messy, not difficult though — just messy.
You can define the first part of the regular expression that finds the sequence "Math.pow(" at any point, and where
you want to allow an arbitrary number of whitespace characters, you can use the sequence \\s* . Recall that \\s
in a Java string specifies the predefined character class \s , which is whitespace. The * quantifier specifies zero or
more of them. If you allow for whitespace between Math.pow and the opening parenthesis for the arguments, and
some more whitespace after the opening parenthesis, the regular expression is:
"(Math.pow)\\s*\\(\\s*"
You have to specify the opening parenthesis by "\\(" . An opening parenthesis is a meta-character, so you have to
write it as an escape sequence.
The opening parenthesis is followed by the first argument, which I said could be a number or a variable name. You
created a regular expression to identify a number earlier:
"[+|-]?(\\d+(\\.\\d*)?)|(\\.\\d+)"
Search WWH ::




Custom Search