DelphiFAQ Home Search:

Using regular expressions (regexs) in Java

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

The java.util.regex package provides two classes for comparing a regular expression against an input String (actually against any CharSequence object2). A Pattern object is a compiled representation of a regular expression3. A Matcher object is an engine that performs match operations on a character sequence by interpreting a Pattern4. The Pattern class contains two static factory methods, compile(String) and compile(String, int), that create and return a Pattern object. The Pattern instance method matcher(CharSequence) creates and returns a Matcher object based on the Pattern instance and the input CharSequence.

Four methods are available to a Matcher object to match part or all of an input String against a regular expression pattern. From the Matcher class documentation4:

The boolean matches() method attempts to match the entire input sequence against the pattern.
The boolean lookingAt() method attempts to match part or all of the input sequence, starting at the beginning, against the pattern.
The boolean find() and boolean find(int) methods scan the input sequence looking for the next subsequence that matches the pattern.
Each of these methods will return true if the pattern matched part or all of the input String as appropriate. As a side effect, the find methods set internal state information in the Matcher object that is used to track the last location where a match occurred.

String input = "She sells sea shells by the sea shore.";

// Create a Pattern object.  A Pattern is a 
// compiled representation of a regular expression.
Pattern pattern = Pattern.compile("shells");

// Create a Matcher object.  A Matcher is an engine that 
// performs match operations on a character sequence by 
// interpreting a Pattern.
Matcher matcher = pattern.matcher(input);

System.out.println(matcher.matches());
// Prints false.  matcher.matches() attempts to
// match the entire input sequence against the pattern.  
// The match would have succeeded, if the pattern described 
// the entire input String.

System.out.println(matcher.lookingAt());
// Prints false.  matcher.lookingAt() attempts to 
// match the input sequence, starting at the beginning, 
// against the pattern.  The match would have succeeded, 
// if the pattern were "She".

System.out.println(matcher.find());
// Prints true.  matcher.find() attempts to find 
// the next subsequence of the input sequence that 
// matches the pattern.

// The Matcher must be reset and searched again after 
// failed attempts at matching.  The Matcher would
// otherwise not be in a proper state to get the 
// information related to the last match and methods 
// that query for such information would throw an 
// IllegalStateException.  Note that the find() method 
// resets the Matcher if previous attempts at matching 
// failed.

System.out.println(matcher.find());   // Prints true.

System.out.println(matcher.group());  // Prints shells.

String[] splits = pattern.split(input);
System.out.println(splits.length);// Prints 2.

for (int i=0; i < splits.length; i++) // Prints 
{   // She sells sea
System.out.println(splits[i]);  //  by the sea shore.
}

pattern = Pattern.compile("Shells");

// Since a new Pattern is to be used, a new Matcher 
// must also be used.
matcher = pattern.matcher(input);

System.out.println(matcher.find()); // Prints false.

Comments:

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: