Java Reference
In-Depth Information
The following snippet of code compiles a regular expression setting the CASE_INSENSTIVE and DOTALL flags,
so the matching will be case-insensitive for US-ASCII charset and the expression. will match a line terminator. For
example, “A@\n” will be matched by the following pattern:
// Prepare a regular expression
String regex = "[a-z]@.";
// Compile the regular expression into a Pattern object setting the
// CASE_INSENSITIVE and DOTALL flags
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
Creating a Matcher
An instance of the
Matcher
class is used to perform a match on a sequence of characters by interpreting the compiled
pattern held in a
Pattern
object. It has no
public
constructor. The
matcher()
method of the
Pattern
class is used to
get an instance of the
Matcher
class. The method takes the string to be matched by the pattern as an argument.
// Create a Pattern object
String regex = "[a-z]@.";
Pattern p = Pattern.compile(regex);
// String to perform the match
String str = "abc@yahoo.com,123@cnn.com,ksharan@jdojo.com";
// Get a matcher object using Pattern object p for str
Matcher m = p.matcher(str);
At this point, the
Matcher
object
m
has associated the pattern represented in the
Pattern
object
p
with the
sequence of characters in
str
. It is ready to start the match operation. Typically, a matcher object is used to find a
match in the sequence of characters. The match may succeed or fail. If the match succeeds, you may be interested in
knowing the start and end positions of the match and the matched text. You can query a
Matcher
object to get all these
pieces of information.
Matching the Pattern
You need to use the following methods of the
Matcher
to perform the match on the input:
The
•
find()
method
•
The
start()
method
•
The
end()
method
group()
method
The
find()
method is used to find a match for the pattern in the input. If the find succeeds, it returns
true
.
Otherwise, it returns
false
. The first call to this method starts the search for the pattern at the beginning of the input.
If the previous call to this method was successful, the next call to this method starts the search after the previous match.
Typically, a call to the
find()
method is used in a while-loop to find all the matches. It is an overloaded method.
Another version of the find() method takes an integer argument, which is the offset to start the find for a match.
The
start()
method returns the start index of the previous match. Typically, it is used after a successful
find()
method call.
•
The