Share on Google+Share on Google+

Matching Pattern using Regularexpression

Advertisement
This Example describe the way to match a pattern with the text by using Regularexpression.

Matching Pattern using Regularexpression

     

This Example describe the way to match a pattern with the text by using Regularexpression.The steps involved in matching a pattern  with the text are described below:

 String ptrn="1\\+1=2":-it is the pattern to define expression (1+1=2) using regularexpression.

 String text="1+1=2":-it is the text which is to be matched with the pattern defined above.

 

 

 

StringConvenience.java

import java.util.regex.*;
public class StringConvenience {

  public static void main(String[] args) {
 String ptrn="1\\+1=2";
 
  String text="1+1=2";
  if(text.matches(ptrn)){
  System.out.println(text+"\t"+ptrn);
  }else{
  System.out.println("no match");
  }
  }
}

output of the program:-

1+1=2 matches  1\+1=2

Download Source code

Advertisements

Advertisement

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

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.

Discuss: Matching Pattern using Regularexpression  

Post your Comment


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