posts - 165, comments - 198, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          java regex 正則部分高級特性使用

          Posted on 2008-05-04 09:59 G_G 閱讀(2271) 評論(3)  編輯  收藏 所屬分類: javaGeneral
          這就不介紹了 代碼上:
          package?unit;

          import?java.util.regex.Matcher;
          import?java.util.regex.Pattern;

          import?org.junit.Assert;
          import?org.junit.Test;

          public?class?RegexUnit?{


          ????@Test
          ????
          /**
          ?????*?<p>向前\向后查找</p>
          ?????
          */
          ????
          public?void?unit9()throws?Exception{
          ????????String?testStr?
          =?"http://www.google.com";
          ????????
          ????????
          /*?一般查找
          ?????????*?.+(:)?查詢出結(jié)果包含?:
          ?????????
          */
          ????????Pattern?pattern?
          =?Pattern.compile(".+(:)");????????
          ????????Matcher?matcher?
          =??pattern.matcher(testStr);
          ????????????Assert.assertTrue(
          "錯誤:?查找出結(jié)果通過?.+(:)?此regex沒有包含?:?",
          ????????????????????matcher.find()?
          &&?matcher.group().equals("http:")?);
          ????????
          ????????
          /*??向前查找
          ?????????*??.+(?=:)?查詢結(jié)果不包含?:
          ?????????
          */
          ????????Pattern?pattern2?
          =?Pattern.compile(".+(?=:)");
          ????????Matcher?matcher2?
          =?pattern2.matcher(testStr);
          ????????????Assert.assertTrue(
          "錯誤:?查找出結(jié)果通過?.+(?=:)?此regex有包含?:?",
          ????????????????????matcher2.find()
          &&?matcher2.group().equals("http"));
          ????????
          /*?向后查找
          ?????????*?(?<=:).+
          ?????????
          */
          ????????Pattern?pattern3?
          =?Pattern.compile("(?<=://).+");
          ????????Matcher?matcher3?
          =?pattern3.matcher(testStr);
          ????????????Assert.assertTrue(
          "錯誤:查找出結(jié)果包含?http://?不向后查詢",
          ????????????????????matcher3.find()
          &&?matcher3.group().equals("www.google.com")?);
          ????}


          ????@Test
          ????
          /**?回朔應(yīng)用?
          ?????*??查詢回朔、回朔替換、回朔大小寫替換
          ?????
          */
          ????
          public?void?unit8()throws?Exception{
          ????????String?testStr?
          =?"this?is?a?block?of?of?test,"+
          ????????????????????????????
          "?several?words?here?are?are?"?+
          ????????????????????????????
          "?repeated?,?and?and?they?should?not?be.?";
          ????????
          ????????Pattern?pattern?
          =?Pattern.compile("[?]+(\\w+)[?]+\\1");
          ????????Matcher?matcher?
          =?pattern.matcher(testStr);
          ????????
          //查詢結(jié)果?are?are?
          ????????Assert.assertTrue("錯誤:regex?不匹配?",?
          ????????????????matcher.find()
          &&matcher.group().split("?").length>=2?);
          ????????
          ????????
          while(?matcher.find()?){
          ????????????Assert.assertTrue(
          "錯誤:regex?不匹配?",?
          ????????????????????matcher.group().split(
          "?").length>=2?);
          ????????}
          ????????
          ????????
          ????????
          //替換
          ????????String?testStr2s?=?"313-555-1234";
          ????????Pattern?pattern2?
          =?Pattern.compile("(\\d{3})(-)(\\d{3})(-)(\\d{4})");
          ????????Matcher?mtmp?
          =??pattern2.matcher(testStr2s);
          ????????Assert.assertTrue(
          "錯誤:沒有查替換",
          ????????????????mtmp.find()?
          &&?
          ????????????????????mtmp.replaceAll(
          "($1)?$3-$5").equals("(313)?555-1234")?);
          ????????
          ????????
          ????????
          /*大小寫替換(java?不能成功)
          ?????????*??\E?結(jié)束?\L?或?\U轉(zhuǎn)換
          ?????????*??\l??\L?把下一個字符(串)換為小寫
          ?????????*??\?u??\U?把下一個字符(串)轉(zhuǎn)換為大寫
          ??? ?? ? *? 此中
          (.*?)懶散加載
          ?????????*/
          ????????String?testStr3?
          =?"tt:google:xx";
          ????????Pattern?pattern3?
          =?Pattern.compile("(?<=:)(.*?)(?=:)");
          ????????Matcher?matcher2?
          =?pattern3.matcher(testStr3);
          ????????
          if(?matcher2.find())
          ????????????System.out.println(?matcher2.group()?)?;
          ????}
          ????
          ????
          }




          評論

          # re: java regex 正則部分高級特性使用  回復(fù)  更多評論   

          2008-05-04 10:17 by lvq810
          不錯 學(xué)習(xí)

          # re: java regex 正則部分高級特性使用  回復(fù)  更多評論   

          2008-05-04 11:50 by Norman
          這些特性都還沒有用過,多謝!

          # re: java regex 正則部分高級特性使用  回復(fù)  更多評論   

          2008-08-11 18:21 by G_G
          匹配多行

          public class Main {
          public static void main(String[] args) {
          String str = "<html>\r\n" +
          "<body>\n" +
          "<h1>title</h1>\r\n" +
          "<h2>title2</h2>\r\n" +
          "</body>\r\n" +
          "</html>\n";
          Pattern pattern = Pattern.compile("<body>[\\S\\s]*?</body>");
          Matcher matcher = pattern.matcher(str);
          while( matcher.find()){
          System.out.println("______________");
          System.out.println( matcher.group() );
          }

          }
          }


          結(jié)果:
          ______________
          <body>
          <h1>title</h1>
          <h2>title2</h2>
          </body>

          http://www.codepub.com/info/2007/02/info-11598-6.html參考
          主站蜘蛛池模板: 苍南县| 黄冈市| 平安县| 永和县| 临沧市| 苏尼特左旗| 沾益县| 延庆县| 黄平县| 通海县| 蚌埠市| 泽州县| 隆子县| 姜堰市| 额济纳旗| 通渭县| 文安县| 郸城县| 昭苏县| 密云县| 衢州市| 青海省| 永州市| 天津市| 乐清市| 乐山市| 隆昌县| 那曲县| 东乡| 高雄县| 麦盖提县| 汉沽区| 榆社县| 南开区| 塔城市| 石屏县| 临朐县| 乌兰察布市| 榆社县| 泾源县| 太和县|