Friday, September 23, 2016

Regular Expression Example




package com.regexTuto;

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

import javax.swing.plaf.synth.SynthSeparatorUI;

public class TestRegularExpression {

public static void main(String args[]){
String s="abwewew";
Pattern pattern = Pattern.compile("^ab$");
boolean p = s.matches("^ab$");
System.out.println("p"+p);
boolean bol = pattern.matcher("ab").matches();
System.out.println(bol);
String s1 = "Nothing can change Nothing you can change Nothing with everything";
String s1Result= s1.replaceAll("$Nothing", "something");
System.out.println(s1Result);
String paySlip = " your Basic Salary is 10000, ? % your other expences is 15000, ur PF is 5000 ";
Pattern patternNumber = Pattern.compile("[0-9]");
System.out.println("10000"+ patternNumber.matcher(paySlip).matches());
Pattern pat_d = Pattern.compile("\\d+"); // find numeric digit
Pattern pat_D = Pattern.compile("\\D+");
Pattern pat_S = Pattern.compile("\\S+");
Pattern pat_s = Pattern.compile("\\s+");
Pattern pat_w = Pattern.compile("\\w+");
Pattern pat_W = Pattern.compile("\\W");
Pattern pat_b = Pattern.compile("\\b+");
      // Matcher m = pat.matcher(paySlip);
       Matcher w_matcher = pat_w.matcher(paySlip);
       Matcher W_matcher = pat_W.matcher(paySlip);
       Matcher s_matcher = pat_s.matcher(paySlip);
       Matcher S_matcher = pat_S.matcher(paySlip);
       Matcher d_matcher = pat_d.matcher(paySlip);
       Matcher D_matcher = pat_D.matcher(paySlip);
       Matcher b_matcher = pat_b.matcher(paySlip);
     
       Matcher Num_matcher = patternNumber.matcher(paySlip);
     
       System.out.println("------------d_matcher-----------------------------");
       while(d_matcher.find()) {
           System.out.println("print :"+d_matcher.group());
       }
       System.out.println("------------D_matcher----------------------------");
       while(D_matcher.find()) {
           System.out.println("print :"+D_matcher.group());
       }
       System.out.println("------------s_matcher-----------------------------");
       while(s_matcher.find()) {
           System.out.println("print :"+s_matcher.group());
       }
       System.out.println("------------S_matcher---------------------------");
       while(S_matcher.find()) {
           System.out.println("print :"+S_matcher.group());
       }
       System.out.println("------------w_matcher-----------------------------");
       while(w_matcher.find()) {
           System.out.println("print :"+w_matcher.group());
       }
       System.out.println("-----------(W_matcher----------------------------");
       while(W_matcher.find()) {
           System.out.println("print :"+W_matcher.group());
       }
       System.out.println("------------------b_matcher-----------------------");
       while(b_matcher.find()) {
           System.out.println("print---- :"+b_matcher.group());
       }

}
}




////////////////////////////////Output//////////////////////////////////////////////////////





pfalse
true
Nothing can change Nothing you can change Nothing with everything
10000false
------------d_matcher-----------------------------
print :10000
print :15000
print :5000
------------D_matcher----------------------------
print : your Basic Salary is 
print :, ? % your other expences is 
print :, ur PF is 
print : 
------------s_matcher-----------------------------
print : 
print : 
print : 
print : 
print : 
print : 
print : 
print : 
print : 
print : 
print : 
print : 
print : 
print : 
print : 
print : 
print : 
------------S_matcher---------------------------
print :your
print :Basic
print :Salary
print :is
print :10000,
print :?
print :%
print :your
print :other
print :expences
print :is
print :15000,
print :ur
print :PF
print :is
print :5000
------------w_matcher-----------------------------
print :your
print :Basic
print :Salary
print :is
print :10000
print :your
print :other
print :expences
print :is
print :15000
print :ur
print :PF
print :is
print :5000
-----------(W_matcher----------------------------
print : 
print : 
print : 
print : 
print : 
print :,
print : 
print :?
print : 
print :%
print : 
print : 
print : 
print : 
print : 
print :,
print : 
print : 
print : 
print : 
print : 
------------------b_matcher-----------------------
print---- :
print---- :
print---- :
print---- :
print---- :
print---- :
print---- :
print---- :
print---- :
print---- :
print---- :
print---- :
print---- :
print---- :
print---- :
print---- :
print---- :
print---- :
print---- :
print---- :
print---- :
print---- :
print---- :
print---- :
print---- :
print---- :
print---- :
print---- :

No comments:

Post a Comment

Required details

--------------------------------------------------------------------------------------------------------------------------- C:\Program File...