Friday, June 13, 2008

Using Regex in java

Replacing brackets in java using Regular Expression.

public static void main (String args[]){
String pattern = "throw new Exception\\("; // pattern
String b = "throw new Exception("; //Match Against
String c = "throw new CustomException(;"; //Replace By

Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(b);
String d = m.replaceAll(c);
System.out.println(d);
boolean bo = m.matches();
System.out.println(bo);
}