Java代码Demo1:
package com.regex; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo1 { public static void main(String[] args) { String regex = "java"; String str = "I lova javascript and I love java"; Pattern pat = Pattern.compile(regex);//Pattern class doesn't have constructor but can be constructed by the compile() factory method. Matcher matcher = pat.matcher(str);//Matcher class doesn't have constructor but can be constructed by the matcher() factory method of Pattern class. if(matcher.matches()){//the matches() method according to the strict matching method. The string must the same as the regex. System.err.println(str + " can match " + regex); }else{ System.err.println(str + " can't match " + regex); } while (matcher.find()) {//the find() method not according to the strict matching method. It will return true if the substring of str match regex. System.err.println(matcher.group());//this method return the String of str which match the regex. System.err.println(str + " can find " + regex); System.err.println(matcher.start());//return the first index in the str which begin match regex. System.err.println(matcher.end());//return the next index in the str match regex. System.err.println(str + " replace all the " + regex + " to " + "python as: " + matcher.replaceAll("python"));//All the String in the str which match regex will be replace to newStr. } } }
运行结果是:
I lova javascript and I love java can't match java java I lova javascript and I love java can find java 7 11 I lova javascript and I love java replace all the java to python as: I lova pythonscript and I love python
java代码Demo2:
package com.regex; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo2 { public static void main(String[] args) { Pattern pat = Pattern.compile("w+"); Matcher mat = pat.matcher("w ww www"); while(mat.find()){ System.err.println(mat.group()); } System.err.println("******************"); pat = Pattern.compile("e.*d");//default to match the longest string mat = pat.matcher("ed end extend"); while(mat.find()){ System.err.println(mat.group()); } System.err.println("******************"); pat = Pattern.compile("e.*?d");//it can match the shorter string mat = pat.matcher("ed end extend"); while(mat.find()){ System.err.println(mat.group()); } System.err.println("******************"); pat = Pattern.compile("[a-zA-Z]+"); mat = pat.matcher("This is a test."); while(mat.find()){ System.err.println(mat.group()); } System.err.println("******************"); pat = Pattern.compile("is"); mat = pat.matcher("This is a test."); if(mat.find()){ System.err.println(mat.replaceAll("was")); } System.err.println("******************"); pat = Pattern.compile("[ ,.!*]"); String str[] = pat.split("This is,a!test*please test.this.");//the split() method not use to generate Matcher class. for(String s:str){ System.err.println("next token: " + s); } System.err.println("******************"); if(Pattern.matches("java","java")){ System.err.println("static boolean matched(String pattern, CharSequence str)");//this method is from Pattern class. } if("java".matches("java")){ System.err.println("boolean matches (String pattern)");//this method can be called by String } } }
运行结果为:
w ww www ****************** ed end extend ****************** ed end extend ****************** This is a test ****************** Thwas was a test. ****************** next token: This next token: is next token: a next token: test next token: please next token: test next token: this ****************** static boolean matched(String pattern, CharSequence str) boolean matches (String pattern)
Java代码BasedRegex:
package com.regex; import java.util.regex.Matcher; import java.util.regex.Pattern; public class BasedRegex { /* * 字符: \xhh 16进制值0xhh 所表示的字符, \t Tab, \n 换行, \r 回车, \f 换页, \e Escape * * 字符集: . 任意一个字符, [abc] a,b,c中的任意一个等价于a|b|c, [a-z&&[hij]] hij中的任意一个字符(交集) \d * 数字 等价于[0-9], \D 非数字 等价于[^0-9], \s 空白符号 等价于[\t\n空格\f\r], \S 非空白字符 * 等价于[^\t\n空格\f\r] \w 一个单词字符 等价于[a-zA-Z_0-9], \W 非单独字符 等价于[^a-zA-Z_0-9] * * 逻辑运算符: XY X 后面跟着 Y, X|Y X或Y, (X) 一个"要匹配的组(capturing group)". * 以后可以用\i来表示第i个被匹配的组 * * 边界匹配符:^ 一行的开头, $ 一行的结束, \b 一个单词的边界, \B 一个非单词的边界, \G 前一个匹配的结束 * * 数量表示符:Greedy(贪婪的): 除非另有表示,否则数量表示符都是greedy的。Greedy的表达式会一直匹配下去,直到匹配不下去为止。 * Reluctant(勉强的): 用问号表示,它会匹配最少的字符。也称为lazy, minimal matching, non-greedy, * 或ungreedy。 Possessive(占有的): * 目前只有Java支持(其它语言都不支持)。它更加先进,所以你可能还不太会用。用正则表达式匹配字符串的时候会产生很多中间状态 * ,(一般的匹配引擎会保存这种中间状态,) 这样匹配失败的时候就能原路返回了。 * 占有型的表达式不保存这种中间状态,因此也就不会回头重来了。它能防止正则表达式的失控,同时也能提高运行的效率。 X? X?? X?+ * 匹配一个或零个X, X* X*? X*+ 匹配零或多个X, X+ X+? X++ 匹配一个或多个X, X{n} X{n}? X{n}+ * 匹配正好n个X, X{n,} X{n,}? X{n,}+ 匹配至少n个X, X{n,m} X{n,m}? X{n,m}+ 匹配至少n个,至多m个X * 要想匹配一个或多个完整的'abc',你应该这样:(abc)+ * * 在其它语言里,"\\ "的意思是"我只是要在正则表达式里插入一个反斜杠。没什么特别的意思。"但是在Java里,"\\ "的意思是 * "我要插入一个正则表达式的反斜杠,所以跟在它后面的那个字符的意思就变了。 * "举例来说,如果你想表示一个或更多的"单词字符",那么这个正则表达式就应该是"\\w+ "。如果你要插入一个反斜杠,那就得用"\\\\ * "。不过像换行,跳格之类的还是只用一根反斜杠:"\n\t"。 */ public static void main(String[] args) { BasedRegex base = new BasedRegex(); base.matchHead("I love javadoc jjava pyjavad java !"); base.mulitSplit("I love*java,and|python!"); base.replaceFirstChars(); base.replaceAllChars(); base.replaceChars(); base.deleteChar(); base.searchChars(); base.testGroup(); } // 查找以Java开头任意字符结尾的字符串 public void matchHead(String match) { Pattern pattern = Pattern.compile("^java.*"); for (String mat : match.split(" ")) { Matcher matcher = pattern.matcher(mat); while (matcher.find()) { System.err.println(matcher.group()); } } System.err.println("************************"); } // 以多条件分割字符串 public void mulitSplit(String str) { /* * 也可以Pattern pattern = Pattern.compile(delimiter);String []strs = * pattern.split(str); */ String delimiter = "[ ,!*|]"; for (String mat : str.split(delimiter)) System.err.println(mat); System.err.println("************************"); } // 文字替换(首次出现字符) public void replaceFirstChars(){ String str = "学习正则表达式学习正则表达式"; // 可以这样:System.err.println(str.replaceFirst("正则表达式", "java")); Pattern pattern = Pattern.compile("正则表达式"); Matcher matcher = pattern.matcher(str); System.err.println(matcher.replaceFirst("java")); System.err.println("************************"); } // 文字替换(全部) public void replaceAllChars(){ String str = "学习正则表达式学习正则表达式"; // 可以这样:System.err.println(str.replaceAll("正则表达式", "java")); Pattern pattern = Pattern.compile("正则表达式"); Matcher matcher = pattern.matcher(str); System.err.println(matcher.replaceAll("java")); System.err.println("************************"); } // 文字替换(置换字符) public void replaceChars(){ String str = "one cat two cats in the yard."; Pattern pattern = Pattern.compile("cat"); Matcher matcher = pattern.matcher(str); StringBuffer sb = new StringBuffer(); while(matcher.find()){ matcher.appendReplacement(sb, "dog"); } matcher.appendTail(sb); System.err.println(sb.toString()); System.err.println(str); } // 去除html标记 public void deleteChar(){ Pattern pattern = Pattern.compile("\\.\\w+"); Matcher matcher = pattern.matcher("<a href = \"index.html\">主页</a>"); String str = matcher.replaceAll(""); System.err.println(str); } // 查找html中对应条件字符串 public void searchChars(){ Pattern pattern = Pattern.compile("href = \".+?\""); Matcher matcher = pattern.matcher("<a href = \"index.html\">主页</a>"); if(matcher.find()){ System.err.println(matcher.group()); } } //group的用法 public void testGroup(){ Pattern pattern = Pattern.compile("(\\d+,)(\\d+)"); Matcher matcher = pattern.matcher("123,456-789,012"); System.err.println("匹配个数: " + matcher.groupCount()); while(matcher.find()){ System.err.println(matcher.group(0)); System.err.println(matcher.group(1)); System.err.println(matcher.group(2)); } } }
运行结果为:
javadoc java ************************ I love java and python ************************ 学习java学习正则表达式 ************************ 学习java学习java ************************ one dog two dogs in the yard. one cat two cats in the yard. <a href = "index">主页</a> href = "index.html" 匹配个数: 2 123,456 123, 456 789,012 789, 012