Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pattern and matcher

 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I got it from ExamLab practice1,

import java.text.*;
import java.util.*;
import java.util.regex.*;
import java.io.*;

public class Expons{

static Stores st;

public static void main(String argv[]){
st=new Stores();
Object ob1=getFormattedObject(Pattern.compile("H[a-d][k-m]"), "H[a-b][k-m]");
Object ob2=getFormattedObject(st.pt1.matcher("Hello Hallo"), "[a-e][l]lo");
Object ob3=getFormattedObject(null);
System.out.println(ob3);
}

public static Object getFormattedObject(Object ob, String... s){
if (ob instanceof Pattern){
st.pt1=((Pattern)ob).compile(s[0]);
return null;
}else if (ob instanceof Matcher){
st.mt1=(Matcher)ob;
return null;
}else{
st.mt1.find();
st.st1=st.mt1.group();
return st.st1;
}
}

}


class Stores{
Pattern pt1; Matcher mt1; String st1;
}



output is: Hal

Can anyone explain me the working of pattern and matcher class with the above arguments.
i am confused which one is regular expression in pattern class and which is string for matcher class....

please clarify this...

Thanks
Preetha
 
Ankit Garg
Sheriff
Posts: 9580
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a nice question! Here's what actually happens in the code

Pattern p = Pattern.compile("H[a-d][k-m]");
Matcher m = p.matcher("Hello Hallo");
m.find();
String s = m.group();
System.out.println(s);

Now that you have an idea what the code actually does, just follow the execution process and you will realize what is happening...
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program flow is complicated to confuse you.

In essence you are just achieving, the result of applying the
expression "H[a-b][k-m]" on the source string "Hello Hallo".

Applying the regex pattern search will give you "Hal" as the matching string from the source.
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ankit,
I am ok with the sample code you gave, the only thing i am not getting is below,

Object ob1=getFormattedObject(Pattern.compile("H[a-d][k-m]"), "H[a-b][k-m]");
Object ob2=getFormattedObject(st.pt1.matcher("Hello Hallo"), "[a-e][l]lo");

and st.pt1=((Pattern)ob).compile(s[0]); // here it has to take s[0] which meant H[a-b][k-m] to match the String.
here i am confused with...

Thanks
Preetha
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Yes..the pattern set here is H[a-b][k-m]..that is what Ankit indicated. It matches 'H' from Hello and 'al' from Hallo...(Lallan Speaking..)
[ December 23, 2008: Message edited by: Himalay Majumdar ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic