Java Reference
In-Depth Information
9.2.7 Matching, Replacing and Splitting by Patterns
Often you will need to write code that validates user input, such as to check whether the input is
a number, a string with all lowercase letters, or a Social Security number. How do you write this
type of code? A simple and effective way to accomplish this task is to use the regular expression.
A regular expression (abbreviated regex ) is a string that describes a pattern for matching a
set of strings. You can match, replace, or split a string by specifying a pattern. This is an
extremely useful and powerful feature.
Let us begin with the matches method in the String class. At first glance, the matches
method is very similar to the equals method. For example, the following two statements
both evaluate to true .
why regular expression?
regular expression
regex
matches(regex)
"Java" .matches( "Java" );
"Java" .equals( "Java" );
However, the matches method is more powerful. It can match not only a fixed string, but also a
set of strings that follow a pattern. For example, the following statements all evaluate to true :
"Java is fun" .matches(
"Java.*"
)
"Java is cool" .matches(
"Java.*"
)
"Java is powerful" .matches(
"Java.*"
)
Java.* in the preceding statements is a regular expression. It describes a string pattern that
begins with Java followed by any zero or more characters. Here, the substring
matches any
.*
zero or more characters.
The following statement evaluates to true .
"440-02-4534" .matches(
"\\d{3}-\\d{2}-\\d{4}"
)
Here \\d represents a single digit, and \\d{3} represents three digits.
The replaceAll , replaceFirst , and split methods can be used with a regular
expression. For example, the following statement returns a new string that replaces $ , + , or #
in a+b$#c with the string NNN .
String s = "a+b$#c" .replaceAll( "[$+#]" , "NNN" );
System.out.println(s);
replaceAll(regex)
Here the regular expression [$+#] specifies a pattern that matches $ , + , or # . So, the output is
aNNNbNNNNNNc .
The following statement splits the string into an array of strings delimited by punctuation
marks.
String[] tokens = "Java,C?C#,C++" .split( "[.,:;?]" );
split(regex)
for ( int i = 0 ; i < tokens.length; i++)
System.out.println(tokens[i]);
In this example, the regular expression [.,:;?] specifies a pattern that matches . , , , : , ; , or
? . Each of these characters is a delimiter for splitting the string. Thus, the string is split into
Java , C , C# , and C++ , which are stored in array tokens .
Regular expression patterns are complex for beginning students to understand. For this rea-
son, simple patterns are introduced in this section. Please refer to Supplement III.H, Regular
Expressions, to learn more about these patterns.
further studies
9.2.8 Finding a Character or a Substring in a String
The String class provides several overloaded indexOf and lastIndexOf methods to find
a character or a substring in a string, as shown in Figure 9.8.
 
Search WWH ::




Custom Search