package regex; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MyPattern { public static void main(String[] args) { /* * Pattern p=Pattern.compile("a*b"); Matcher m=p.matcher("aaaab"); * boolean b=m.matches(); */ boolean b = Pattern.matches("a*bb", "aaaabb"); // *表示的是數量(它指定*前邊的內容能夠連續重複使用任意次以使整個表達式獲得匹配)。 System.out.println(b); /* * String str = "2011-11-12"; String pat = "\\d{4}-\\d{2}-\\d{2}"; * System.out.println(Pattern.compile(pat).matcher(str).matches()); */ // 上面三句至關於下面一句 System.out.println("日期匹配:"+Pattern .matches("\\d{4}-\\d{2}-\\d{2}", "2015-07-27")); //主要轉義字符\ (\就變成\\ , .就成爲\.) System.out.println("電話號匹配:"+Pattern .matches("0\\d{3}-\\d{8}", "0571-78771111")); System.out.println("手機號匹配:"+Pattern .matches("^[1]([3][0-9]{1}|51|59|58|88|89)[0-9]{8}$", "15111115555")); System.out.println("多匹配:"+Pattern.matches("\\bhello.*world\\b", "hello...........world")); } }
package regex; import java.util.regex.Pattern; public class RegexText { public static void main(String[] args) { Pattern pattern=Pattern.compile("[/]+"); String[] result =pattern.split("哈哈哈哈" + "/ 呵呵呵呵" + "嘿嘿嘿嘿/嘻嘻嘻嘻"); for (String string : result) { System.out.println(string); System.out.println("********************************************************"); } System.out.println("=============================================================="); String[] strings =pattern.split("哈哈哈哈" + "/ 呵呵呵呵" + "嘿嘿嘿嘿/嘻嘻嘻嘻",2); for (String string : strings) { System.out.println(string); System.out.println("********************************************************"); } } }
參考:http://www.yq1012.com/api/
html
http://www.cnblogs.com/deerchao/archive/2006/08/24/zhengzhe30fengzhongjiaocheng.html
java