新的起點 新的開始

          快樂生活 !

          深入淺出Java正則表達(1)

              正則表達式作為Perl最出眾的賣點,已經在Linux平臺上廣泛的應用,比如命令grep sed等。JDK1.4 在NIO中重要提供了正則表達式引擎的實現。本系列將通過解決問題的方式來深入學習Java正則表達式。
          最終我們目標是如何通過正則表達式獲取Java代碼中方法的個數與屬性的個數(當然通過反射是小Case)
          (1)如何統計一行中重復單詞
           如果沒有正則表達式,我們只能解析逐個判斷了。正則呢?
          import java.util.regex.Matcher;
          import java.util.regex.Pattern;
          import java.util.regex.PatternSyntaxException;

          /**
           *
           * 
          @author vma
           
          */
          public class MatchDuplicateWords {
           
          public static void main(String args[]){
                hasDuplicate(
          "Faster pussycat  haha haha  dd dd haha haha zz zz");
             }

             
          public static boolean hasDuplicate(String phrase){
                
          boolean retval=false;

                String duplicatePattern 
          ="\\b(\\w+) \\1\\b";
                Pattern p 
          = null;
                
          try{
                  p 
          = Pattern.compile(duplicatePattern);
                }
                
          catch (PatternSyntaxException pex){
                   pex.printStackTrace();
                   System.exit(
          0);
                }
                
          //count the number of matches.
                int matches = 0;
                
          //get the matcher
                Matcher m = p.matcher(phrase);
                String val
          =null;

                
          //find all matching Strings
                while (m.find()){
                   retval 
          = true;
                  val 
          = ":" + m.group() +":";
                  System.out.println(val);
                  matches
          ++;
                }

                
          //prepare a message indicating success or failure
                String msg = "   NO MATCH: pattern:" + phrase
                       
          + "\r\n             regex: "
                       
          + duplicatePattern;

                
          if (retval){
                msg 
          = " MATCH     : pattern:" + phrase
                    
          + "\r\n         regex: "
                    
          + duplicatePattern;
                }

                System.out.println(msg 
          +"\r\n");
                
          return retval;
             }
          }


          posted on 2008-08-24 00:13 advincenting 閱讀(482) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          公告

          Locations of visitors to this pageBlogJava
        1. 首頁
        2. 新隨筆
        3. 聯系
        4. 聚合
        5. 管理
        6. <2008年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          統計

          常用鏈接

          留言簿(13)

          隨筆分類(71)

          隨筆檔案(179)

          文章檔案(13)

          新聞分類

          IT人的英語學習網站

          JAVA站點

          優秀個人博客鏈接

          官網學習站點

          生活工作站點

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 高阳县| 长治市| 双柏县| 佛学| 百色市| 朝阳县| 临桂县| 大厂| 赞皇县| 花莲县| 台江县| 和政县| 阜康市| 田林县| 孝昌县| 霸州市| 报价| 汕头市| 大同县| 英吉沙县| 鲁甸县| 临沭县| 彭阳县| 武平县| 来宾市| 航空| 当阳市| 收藏| 北宁市| 越西县| 明星| 沭阳县| 宜黄县| 贵港市| 浦北县| 华阴市| 大关县| 堆龙德庆县| 准格尔旗| 固安县| 崇礼县|