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



Home Regularexpressions Pointing and indicating matcher using regular expression

 
 

Share on Google+Share on Google+

Pointing and indicating matcher using regular expression

Advertisement
This Example describes the way to point the matcher and also indicate the index of the matcher using regular expression. For this we are going to make program named Indicating_Matcher.java.

Pointing and indicating matcher using regular expression

     

This Example describes the way to point the matcher and also indicate the index of the matcher using regular expression. For this we are going to make program named Indicating_Matcher.java. The steps involved in program Indicating_Matcher.java are described below:-

String text = "My name is Tim.In October Tim will arrive.":- Declaring text as String in which first index of word Tim is to be pointed out.

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

String matcher[]={" ^","  ^"}:- Creates an array name 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.

Indicating_Matcher.java

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Indicating_Matcher {

  public static void main(String[] args) {
 String text = "My name is Tim.In October Tim will arrive.";
   
  Pattern p=Pattern.compile("Tim");
  String matcher[]={"  ^","  ^"};
 
  Matcher m=p.matcher(text);
  m.find();
  int index=m.start();
  System.out.println(text);
  System.out.println(matcher[0]+index);
  
  m.find();
  int index1=m.start();
  System.out.println(text);
  System.out.println(matcher[1]+index1);
  }
}

Output of the program:-

My name is Tim.In October Tim will arrive.
   ^11
My name is Tim.In October Tim will arrive.
       ^26

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 29, 2008

Related Tutorials

Discuss: Pointing and indicating matcher using regular expression  

Post your Comment


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