Matching Pattern using Regularexpression

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

Matching Pattern 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