package shb.java.demo3; import java.util.regex.Matcher;
import java.util.regex.Pattern; /**
* 正则表达式简单操作
* @Package:shb.java.demo3
* @Description:
* @author shaobn
* @Date 2015-8-28上午10:54:30
*/
public class RegexDemo2 {
public static void main(String[] args) {
ifTrue("[email protected]", "[0-9]{8,11}@(\\w*|[0-9]+)\\.[a-z]*");
splitRegex("C:\\demo\\txt", "\\\\");
ifTrue("Abc bbb", "\\AAbc\\b");
ifTrue("131222", "(13|2|3).{2,4}2$");
ifTrue("java", "^j.*a$");
ifTrue("javaa", "java*");
ifTrue("aBc123", "[a-zA-Z]{3}\\d{3,9}");
ifTrue("13635586954", "[1][3,5,8]\\d{9}");
ifTrue("13635586954", "[1][3,5,8]\\d*");
splitRegex("The day is Friday", " +");
splitRegex("TTddayiisFFriday", "(.)\\1");
}
//根据正则表达式匹配字符串
public static void ifTrue(String str,String regex){
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
boolean flag = m.matches();
System.out.println(flag);
}
//根据正则表达式切割字符串
public static void splitRegex(String str,String regex){
String[] strs = str.split(regex);
for(String s:strs){
System.out.println(s);
} }
//根据正则表达式切割字符串
public static void splitRegex2(){
Pattern pattern = Pattern.compile("[, |]+");
String[] strs = pattern.split("Java Hello World Java,Hello,,World|Sun");
for (int i=0;i<strs.length;i++) {
System.out.println(strs[i]);
}
}
}