Java Reference
In-Depth Information
13
System.out.println(
"Enter a sentence and press Enter"
);
14
String sentence = scanner.nextLine();
15
16
// process user sentence
17
String[] tokens = sentence.split(
" "
);
18
System.out.printf(
"Number of elements: %d%nThe tokens are:%n"
,
19
tokens.length);
20
21
for
(String token : tokens)
System.out.println(token);
22
23
}
24
}
// end class TokenTest
Enter a sentence and press Enter
This is a sentence with seven tokens
Number of elements: 7
The tokens are:
This
is
a
sentence
with
seven
tokens
Fig. 14.18
|
StringTokenizer
object used to tokenize strings. (Part 2 of 2.)
Matcher
A
regular expression
is a
String
that describes a
search pattern
for
matching
characters in
other
String
s. Such expressions are useful for
validating input
and ensuring that data is in
a particular format. For example, a ZIP code must consist of five digits, and a last name
must contain only letters, spaces, apostrophes and hyphens. One application of regular ex-
pressions is to facilitate the construction of a compiler. Often, a large and complex regular
expression is used to
validate the syntax of a program
. If the program code does
not
match
the regular expression, the compiler knows that there's a syntax error in the code.
Class
String
provides several methods for performing regular-expression operations,
the simplest of which is the matching operation.
String
method
matches
receives a
String
that specifies the regular expression and matches the contents of the
String
object
on which it's called to the regular expression. The method returns a
boolean
indicating
whether the match succeeded.
A regular expression consists of literal characters and special symbols. Figure 14.19
specifies some
predefined character classes
that can be used with regular expressions. A
character class is an
escape sequence
that represents a group of characters. A digit is any
numeric character. A
word character
is any letter (uppercase or lowercase), any digit or the
underscore character. A white-space character is a space, a tab, a carriage return, a newline
or a form feed. Each character class matches a single character in the
String
we're
attempting to match with the regular expression.