Pattern resetting using regular expression

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

Pattern resetting using regular expression

     

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

String regex = "[frb][aiu]":-Defining regression in string form, Here "[] []" determines text and finds parts that match the provided specification.

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

String text = "fix the rug ":-Declaring text from where match will be provided.

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

(matcher.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.

Pattern_Resetting.java

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

public class Pattern_Resetting {

  public static void main(String[] args) throws Exception {
  String regex = "[frb][aiu]";
  Pattern pattern = Pattern.compile(regex);
 
  String text = "fix the rug ";
  
  Matcher matcher = pattern.matcher(text);
  while (matcher.find()) {
 System.out.println(matcher.group());
  }
  matcher.reset("fix the rig with rags");
  while (matcher.find()) {
  System.out.println(matcher.group());
  }
  }

Output of the program:-

fi ru fi ri


Download Source Code

Advertisements

Share on Google+Share on Google+

Pattern resetting using regular expression

Posted on: August 30, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Pattern resetting using regular expression  

Post your Comment


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

Ads

 

Ads