Pages

Sunday, July 05, 2009

Regular expression in JAVA

I found pattern matching in JAVA very simple and easy. Till now I just know two class, One is Pattern and the other is Matcher. With this two class I am working fine. and the code is also very simple. One thing to remember none of the two class cant be initiated with the new operator.

Code is something like this.

String regx = "my reg X";
String input = "here put your input string";
Pattern pattern = Pattern.compile(regx); 
Matcher matcher = pattern.matcher(input);
Now you have got your parsing engine ready. you have to just search now.

while (matcher.find()) {                 System.out.println("Match found");
  System.out.println("Start" + matcher.start());
  System.out.println("Start" + matcher.end());
}
Now stop using StringTokenizer and all other complex logics to parse something.

No comments:

Post a Comment