java正则表达式练习
1 package shb.java.demo3; 2 3 import java.util.regex.Matcher; 4 import java.util.regex.Pattern; 5 6 /** 7 * 正则表达式简单操作 8 * @Package:shb.java.demo3 9 * @Description: 10 * @author shaobn 11 * @Date 2015-8-28上午10:54:30 12 */ 13 public class RegexDemo2 { 14 public static void main(String[] args) { 15 ifTrue("644050655@qq.com", "[0-9]{8,11}@(\\w*|[0-9]+)\\.[a-z]*"); 16 splitRegex("C:\\demo\\txt", "\\\\"); 17 ifTrue("Abc bbb", "\\AAbc\\b"); 18 ifTrue("131222", "(13|2|3).{2,4}2$"); 19 ifTrue("java", "^j.*a$"); 20 ifTrue("javaa", "java*"); 21 ifTrue("aBc123", "[a-zA-Z]{3}\\d{3,9}"); 22 ifTrue("13635586954", "[1][3,5,8]\\d{9}"); 23 ifTrue("13635586954", "[1][3,5,8]\\d*"); 24 splitRegex("The day is Friday", " +"); 25 splitRegex("TTddayiisFFriday", "(.)\\1"); 26 } 27 //根据正则表达式匹配字符串 28 public static void ifTrue(String str,String regex){ 29 Pattern p = Pattern.compile(regex); 30 Matcher m = p.matcher(str); 31 boolean flag = m.matches(); 32 System.out.println(flag); 33 } 34 //根据正则表达式切割字符串 35 public static void splitRegex(String str,String regex){ 36 String[] strs = str.split(regex); 37 for(String s:strs){ 38 System.out.println(s); 39 } 40 41 } 42 //根据正则表达式切割字符串 43 public static void splitRegex2(){ 44 Pattern pattern = Pattern.compile("[, |]+"); 45 String[] strs = pattern.split("Java Hello World Java,Hello,,World|Sun"); 46 for (int i=0;i<strs.length;i++) { 47 System.out.println(strs[i]); 48 } 49 } 50 }
吾宁做一叶扁舟,始航于湖边,遨游于海上,浪迹于江中。