import java.util.regex.Matcher import java.util.regex.Pattern public class ReTest { // : public static void main(String args[]){ //1、 matches() // Java , Pattern pattern1=Pattern.compile("^Java.*") Matcher matcher1=pattern1.matcher("Java ") boolean b=matcher1.matches() System.out.println(b) //2、 split() Pattern pattern2=Pattern.compile("[,|]+") String[] strs=pattern2.split("Java Hello World Java,Hello,,World|Sun") for(int i=0 System.out.print(strs[i]+" ") }//Java Hello World Java Hello World Sun System.out.println() //3、 ( )replaceFirst() Pattern pattern3 = Pattern.compile(" ") Matcher matcher3 = pattern3.matcher(" Hello World, Hello World") // System.out.println(matcher3.replaceFirst("Java")) //Java Hello World, Hello World //4、 ( )replaceAll() Pattern pattern4 = Pattern.compile(" ") Matcher matcher4 = pattern4.matcher(" Hello World, Hello World") // System.out.println(matcher4.replaceAll("Java")) //Java Hello World,Java Hello World //5、 ( )appendReplacement()、appendTail() Pattern pattern5 = Pattern.compile(" ") Matcher matcher5 = pattern5.matcher(" Hello World, Hello World ") StringBuffer sbr = new StringBuffer() while (matcher5.find()) { matcher5.appendReplacement(sbr, "Java") } System.out.println(sbr) matcher5.appendTail(sbr) System.out.println(sbr) //6、 String str="[email protected]" //Pattern.CASE_INSENSITIVE: Pattern pattern6 = Pattern.compile("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+",Pattern.CASE_INSENSITIVE) Matcher matcher6 = pattern6.matcher(str) System.out.println(matcher6.matches()) //true //7、 html //+? ,Pattern.DOTALL: Pattern pattern7 = Pattern.compile("<.+?>", Pattern.DOTALL) Matcher matcher7=pattern7.matcher("<a href=\"index.html\"> </a>") String string = matcher7.replaceAll("") System.out.println(string) //8、 html //() ,0 ,1 Pattern pattern8 = Pattern.compile("href=\"(.+?)\"") Matcher matcher8 = pattern8.matcher("<a href=\"index.html\"> </a>") if(matcher8.find()){// System.out.println(matcher8.group(0)) System.out.println(matcher8.group(1)) } //9、 http:// Pattern pattern9 = Pattern.compile("(http://|https://){1}[\\w[.-/]:]+") Matcher matcher9 = pattern9.matcher("dsdsds<http://dsds//gfgffdfd>fdf") StringBuffer buffer = new StringBuffer() while(matcher9.find()){ buffer.append(matcher9.group()) buffer.append("\r
") } System.out.println(buffer.toString()) //10、 {} String st = "Java {0} -{1} " String[][] object={new String[]{"\\{0\\}","1995"},new String[]{"\\{1\\}","2007"}} for(int i=0 String[] result=object[i] Pattern pattern = Pattern.compile(result[0]) Matcher matcher = pattern.matcher(st) st=matcher.replaceAll(result[1]) } System.out.println(st) } }