import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Test;
public class TestRegularExp {
@Test
public void testSearch(){
String str = "lingyibin";
String regExp = "n*y";
Pattern pattern = Pattern.compile(regExp);//,Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
boolean b = matcher.find();
System.out.println(b);
System.out.println(matcher.start() + "--->" + matcher.end());
}
@Test
public void testGetSubStr(){
String str = "E:\\Eclipse3_6\\workspace\\Test.txt";
//We'll have to use four '\' instead of one
String regExp = ".+\\\\(.+)$";//"\\\\.+\\\\";
Pattern pattern = Pattern.compile(regExp);
Matcher matcher = pattern.matcher(str);
matcher.find(); //This is could not be deleted! Or you'll get an error!
int cnt = matcher.groupCount();
for(int i = 0; i <= cnt; i ++){
System.out.println(matcher.group(i));
}
}
@Test
public void testSplit(){
String str = "ling#yi#bin";
String regExp = "#";
Pattern pattern = Pattern.compile(regExp);
String[] strs = pattern.split(str);
for(String s:strs){
System.out.println(s);
}
}
@Test
public void testReplace(){
String str = "lllling";
String regExp = "l+";
Pattern pattern = Pattern.compile(regExp);
Matcher matcher = pattern.matcher(str);
str = matcher.replaceAll("L");
System.out.println(str);
}
}