新的起點 新的開始

          快樂生活 !

          深入淺出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 閱讀(481) 評論(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站點

          優秀個人博客鏈接

          官網學習站點

          生活工作站點

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 科技| 台江县| 遂川县| 萨嘎县| 马龙县| 西充县| 万源市| 湾仔区| 本溪| 涿鹿县| 古交市| 红桥区| 教育| 南宁市| 义乌市| 镇安县| 图木舒克市| 彭阳县| 凤城市| 兴义市| 湖州市| 错那县| 松溪县| 行唐县| 清水河县| 光山县| 永昌县| 呼伦贝尔市| 湛江市| 景宁| 阿克陶县| 滨州市| 绵阳市| 宜君县| 高邑县| 湟源县| 加查县| 桃江县| 定陶县| 宁安市| 塘沽区|