Java Reference
In-Depth Information
How It Works
We first define a string,
regEx
, containing the regular expression, and a string,
str
, that we will search.
We also create an array of type
char[]
that we use to indicate where the pattern is found in the string.
We fill the elements of this array with spaces initially using the static
fill()
method from the
Arrays
class that we discussed earlier. Later we will replace some of these spaces with '
^
' to indicate where the
pattern has been found.
Once we have compiled the regular expression
regEx
into a
Pattern
object,
pattern
, we create a
Matcher
object,
m
, from
pattern
that applies to the string
str
. We then call the
find()
method for
m
in the
while
loop condition. This loop will continue as long as the
find()
method returns
true
.
On each iteration we output the index values returned by the
start()
and
end()
methods that reflect
the index position where the first character of the pattern was found and the index position following
the last character. We also insert the '
^
' character in the marker array at the index positions where the
pattern was found - again using the
fill()
method.
When the loop ends we have found all occurrences of the pattern in the string so we output the string
str
, with the contents of the marker array immediately below it on the next line. As long as we are
using a fixed width font for output to the command line, the '
^
' characters will mark the positions where
the pattern appears in the string.
We will reuse this example as we delve into further options for regular expressions by plugging in different
definitions for
regEx
and the string that is searched,
str
. The output will be more economical if you delete
or comment out the statement in the
while
loop that outputs the start and end index positions.
Matching an Entire String
There are occasions when you want to try to match a pattern against an entire string, in other words
when you want to establish that the complete string that you are searching is a match for the pattern.
Suppose you read an input value into your program as a string. This might be from the keyboard or
possibly through a dialog box managing the data entry. You might want to be sure that the input string
is an integer for example. If input should be of a particular form, you can use a regular expression to
determine whether it is correct or not.
The
matches()
method for a
Matcher
object tries to match the entire input string with the pattern and
returns
true
only if there is a match. We can illustrate how this works with the following code fragment:
String input = null;
// Read into input from some source...
Pattern yes = Pattern.compile("yes");
Matcher m = pattern.matcher(input);
if(m.matches()) // Check if input matches "yes"
System.out.println("Input is yes.");
else
System.out.println("Input is not yes.");
Of course, this illustration is trivial, but later we will see how to define more sophisticated patterns that
can check for a range of possible input forms.