java正則表達式可算知道些了

import static net.mindview.util.Print.*;
import java.util.regex.*;

public class TestRegularExpress
{
   public static void main(String[] args)
   {
       String s = "a88897a";
       print(s.matches("..."));
       print(s);
       print(s.replaceAll("//d", "-"));
      
       print();
       print(" /r/n/t".matches("//s{4}"));
       print("".matches("//S"));
       print("a_8".matches("//w{3}"));
       print("abc888&^%".matches("[a-z]{1,3}//d+[&^%#]+"));
       print("//".matches("////"));
       print("192.168.25.8".matches("//d+//.//d+//.+//d.+//d+"));
      
       print();
       print("a".matches("[abc]"));
       print("a".matches("[^abc]"));
       print("A".matches("[a-zA-Z]"));
       print("A".matches("[a-z]|[A-Z]"));
       print("A".matches("[a-z[A-Z]]"));
       print("R".matches("[A-Z[RFJ]]"));
      
       print();
       print("a".matches("//p{Lower}"));
      
       print();
       print("hello sir".matches("^h.*"));
       print("hello sir".matches(".*ir$"));
       print("hello sir".matches("^h[a-z]{1,3}0//b.*"));
       print("hello sir".matches("^h[a-z]{1,3}0//b.*"));
       print(" /n".matches("^[//s&&^//n]*//n$"));
      
       print();
       Pattern p = Pattern.compile("//d{3,5}");
       s = "123-34345-234-00";
       Matcher m = p.matcher(s);
       print(m.matches());
       m.reset();
       print(m.find());
       print(m.start() + "-" + m.end());
       print(m.find());
       print(m.start() + "-" + m.end());
       print(m.find());
       print(m.start() + "-" + m.end());
       print(m.find());
             
       print();
       print(m.lookingAt());
       print(m.lookingAt());
       print(m.lookingAt());
       print(m.lookingAt());
      
       print();
       print("[email protected]".matches("[//w[.-]]+@[//w[.-]]+//.[//w]+"));
      
       Pattern pp = Pattern.compile("java",Pattern.CASE_INSENSITIVE);
       Matcher mm = pp.matcher("java Java JaVa JAVa JAva Ilove Java abadfefwee");
       //print(mm.replaceAll("JAVA"));   //java全被換爲JAVA
       StringBuffer buffer = new StringBuffer();
        int i = 0;
       while(mm.find())
       {        
         i++;
         if(i%2==0)
           mm.appendReplacement(buffer, "java");
         else
           mm.appendReplacement(buffer, "JAVA");
       }
        mm.appendTail(buffer);
        print(buffer);
     
     print(); 
     pp = Pattern.compile("(//d{3})([a-z]{2})");
     s = "123aa-354bb-454cc-00";
      m = pp.matcher(s);
     while(m.find())
     {
      print(m.group(1));
      print(m.group(2));
      print(m.group());
     }
    
     print();
     s = "aaaa5bbbb6";
     pp = Pattern.compile("(.{3,10}+)[0-9]");
     m = pp.matcher(s);
     if(m.find())
         print(m.start() + "---" + m.end());
     else
         print("not match");
   }
}
   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章