Java Reference
In-Depth Information
Running the code now produces the result shown in Figure 6-3.
figureĀ 6-3
This looks as if it has all gone horribly wrong. The regular expression has matched the
Paul
substrings at the start and the end of the string, and the penultimate
paul
, just as you wanted.
However, the
Paul
substrings inside
Pauline
and
Paula
have also been replaced.
The
RegExp
object has done its job correctly. You asked for all patterns of the characters
Paul
to be
replaced and that's what you got. What you actually meant was for all occurrences of
Paul
, when
it's a single word and not part of another word, such as
Paula
, to be replaced. The key to making
regular expressions work is to define exactly the pattern of characters you mean, so that only that
pattern can match and no other. So let's do that.
1.
You want
paul
or
Paul
to be replaced.
2.
You don't want it replaced when it's actually part of another word, as in
Pauline
.
How do you specify this second condition? How do you know when the word is joined
to other characters, rather than just joined to spaces or punctuation or the start or end of the
string?
To see how you can achieve the desired result with regular expressions, you need to enlist the help of
regular expression special characters. You look at these in the next section, by the end of which you
should be able to solve the problem.