Java Reference
In-Depth Information
FIGURE 4.11
A regular expression that contains character classes
Next must come
1 on more white
space characters
The sequence must
end with 1 or more
word characters.
[A-Z] \\w
*
\\s
[A-Z] \\w
The sequence must
start with a single
uppercase letter
between A and Z
Next can come
any number
(including 0) of
word characters
Next must come
an uppercase
letter between
A and Z
The string
“JohnDoe”
is not a match because it does not contain any whitespace charac-
ters.
“John doe”
is not a match because the second word does not start with a letter from
A
to
Z
.
“J D”
is not a match because the
D
is not followed by one or more word characters.
The other strings in the
names
array match, so the output of the code is
John Doe matches [A-Z]\w*\s+[A-Z]\w+
John Doe matches [A-Z]\w*\s+[A-Z]\w+
J D5 matches [A-Z]\w*\s+[A-Z]\w+
As you can see, regular expressions can become quite complex, but their complexity
makes them a powerful tool for matching character sequences. They also show up in other
Java API classes and methods, including the
String.split
method, which we discuss next.
The
String.split
Method
The
String
class contains a method named
split
that takes in a regular expression and
splits the
String
object into an array of
String
objects. The signature of the
split
method
is
public String [] split(String regex)
.
The
String
argument is a regular expression, and the return value is one or more
String
objects in an array. The size of the array depends on how many matches of the regular
expression are found. For example, the following statements split a
String
object into three
String
objects:
String greetings = “hi;hello;welcome”;
String [] greetingsArray = greetings.split(“;”);
for(String greeting : greetingsArray) {
System.out.println(greeting);
}
The regular expression in the call to
split
is the string literal
“;”
that appears twice in
the
greetings String
. The
greetingsArray
contains three elements. The output of the
code is
Search WWH ::
Custom Search