Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Regularexpressions Reset the matcher using regular expression

 
 

Share on Google+Share on Google+

Reset the matcher using regular expression

Advertisement
This Example describes the way to retrieve the String using regular expression. For this we are going to make program named Reset. java.

Reset the matcher using regular expression

     

This Example describes the way to retrieve the String using regular expression. For this we are going to make program named Reset. java. The steps involved in program Reset. java are described below:-

String regex = ("\\w.+"):- Creating expression for text. Here "\\w" means to match any alphanumeric character.

Pattern p = Pattern.compile(regex):- Creating a pattern object and compiles the given regular expression into a pattern.

String text = "Roseindia.net Rohini":- Declaring text which is to be reset by the matcher.

Matcher m = p.matcher(text):- Creates a matcher for matching the given input against pattern p.

m.find():- This is the method of matcher class which is used for finding the next subsequence of the input sequence and it usually returns a boolean value.

m.reset():- This is the method of the class Matcher which is used to reset the matcher.

Reset.java

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Reset {

  public static void main(String[] args) {
  String regex = ("\\w.+");
  Pattern p = Pattern.compile(regex);
  String text = "Roseindia.net Rohini";
  Matcher m = p.matcher(text);
  System.out.print("Text before resetting the Matcher is: ");
  while (m.find()) {
  System.out.print(m.group());
  }
  m.reset();
  System.out.println();
  System.out.print("Text after resetting the Matcher is: ");
  while (m.find()) {
  System.out.print(m.group());
  }

  }
}

Output of the program:-

Text before resetting the Matcher is: Roseindia.net Rohini
Text after resetting the Matcher is: Roseindia.net Rohini


Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 30, 2008

Related Tutorials

Discuss: Reset the matcher using regular expression  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com