Java Reference
In-Depth Information
acter of the pattern was found, and the index position following the last character. You also insert the
'^' character in the marker array at the index positions where the pattern was found — again using the
fill() method. The loop ends when the find() method returns false , implying that there are no more
occurrences of the pattern in the string.
When the loop ends you have found all occurrences of the pattern, so you output str with the contents
of the marker array immediately below it. As long as the command-line output uses a fixed-width font,
the '^' characters mark the positions where the pattern appears in the string.
You reuse this example as you delve into further options for regular expressions by plugging in different
definitions for regEx and the string that is searched, str . The output is 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
On some occasions you want to try to match a pattern against an entire string — in other words, you want
to establish that the complete string you are searching is a match for the pattern. Suppose you read an in-
put value into your program as a string. This might be from the keyboard or possibly through a dialog box
managing the entry data in the graphical user interface for an application. 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 expres-
sion 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. The following code fragment demonstrates how this works:
String input = null;
// Read into input from some source...
Pattern yes = Pattern.compile("yes");
Matcher m = yes.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 you see how you can define more sophisticated patterns that
can check for a range of possible input forms.
Defining Sets of Characters
A regular expression can be made up of ordinary characters, which are upper- and lowercase letters and di-
gits, plus sequences of meta-characters , which are characters that have a special meaning. The pattern in
the previous example was just the word "had" , but what if you wanted to search a string for occurrences of
"hid" or "hod" as well as "had" , or even any three-letter word beginning with "h" and ending with "d" ?
You can deal with any of these possibilities with regular expressions. One option is to specify the middle
character as a wildcard by using a period; a period is one example of a meta-character. This meta-character
matches any character except end-of-line, so the regular expression "h.d" , represents any sequence of three
Search WWH ::




Custom Search