tinguo002

           

          java正則表達(dá)式 提取、替換(轉(zhuǎn))

          java正則表達(dá)式 提取、替換

          事例1
          http://www.cnblogs.com/lihuiyy/archive/2012/10/08/2715138.html
          比如,現(xiàn)在有一個(gè) endlist.txt 文本文件,內(nèi)容如下:
          1300102,北京市
          1300103,北京市
          1300104,北京市
          1300105,北京市
          1300106,北京市
          1300107,北京市
          1300108,北京市
          1300109,北京市
          1300110,北京市
          1300111,北京市
          1300112,北京市
          1300113,北京市
          1300114,北京市
          1300115,北京市
          1300116,北京市
          1300117,北京市
          1300118,北京市
          1300119,北京市
          七位數(shù)字代表手機(jī)號(hào)碼的前七位,后面的漢字表示號(hào)碼歸屬地。現(xiàn)在我要將這些內(nèi)容按照 130 131 132...  開頭分別寫到 130.txt 131.txt 132.txt.....這些文件中。
          public static void main(String args[]) {
                   File file 
          = null;
                   BufferedReader br 
          = null;
                   StringBuffer buffer 
          = null;
                   String childPath 
          = "src/endlist.txt";
                   String data 
          = "";
                   
          try {
                       file 
          = new File(childPath);
                       buffer 
          = new StringBuffer();
                       InputStreamReader isr 
          = new InputStreamReader(new FileInputStream(file), "utf-8");
                       br 
          = new BufferedReader(isr);
                       
          int s;
                       
          while ((s = br.read()) != -1{
                           buffer.append((
          char) s);
                       }

                       data 
          = buffer.toString();
                   }
           catch (Exception e) {
                       e.printStackTrace();
                   }

                   Map
          <String, ArrayList<String>> resultMap = new HashMap<String, ArrayList<String>>();
                   
          for (int i = 0; i < 10; i++{
                       resultMap.put(
          "13" + i, new ArrayList<String>());
                   }

           
                  Pattern pattern = Pattern.compile("(\\d{3})(\\d{4},[\u4e00-\u9fa5]*\\n)");
                   Matcher matcher = pattern.matcher(data);          
          while (matcher.find()) {
                       resultMap.get(matcher.group(
          1)).add(matcher.group(2));
                   }

                   
          for (int i = 0; i < 10; i++{
                       
          if (resultMap.get("13" + i).size() > 0{
                           
          try {
                               File outFile 
          = new File("src/13" + i + ".txt");
                               FileOutputStream outputStream 
          = new FileOutputStream(outFile);
                               OutputStreamWriter writer 
          = new OutputStreamWriter(outputStream, "utf-8");
                               ArrayList
          <String> tempList = resultMap.get("13" + i);
                               
          for (int j = 0; j < tempList.size(); j++{
                                   writer.append(resultMap.get(
          "13" + i).get(j));
                               }

                               writer.close();
                               outputStream.close();
                           }
           catch (Exception e) {
                               
          // TODO Auto-generated catch block
                               e.printStackTrace();
                           }

                       }

                   }

               }

          第24行使用正則表達(dá)式  "(\\d{3})(\\d{4},[\u4e00-\u9fa5]*\\n)" 每個(gè)()中的內(nèi)容為一組,索引從 1 開始,0表示整個(gè)表達(dá)式。所以這個(gè)表達(dá)式分為兩組,第一組表示3個(gè)數(shù)字,第二組表示 4個(gè)數(shù)字加多個(gè)漢字加一個(gè)換行符。提取時(shí)如26-28行所示。



          事例2
          http://www.cnblogs.com/jxgxy/archive/2012/07/30/2615997.html
          import java.util.ArrayList;
          import java.util.regex.Matcher;
          import java.util.regex.Pattern;

          public class test {
              
          public static void main(String[] args) {
                  getStrings(); 
          //用正則表達(dá)式獲取指定字符串內(nèi)容中的指定內(nèi)容
                  System.out.println("********************");
                  replace(); 
          //用正則表達(dá)式替換字符串內(nèi)容
                  System.out.println("********************");
                  strSplit(); 
          //使用正則表達(dá)式切割字符串
                  System.out.println("********************");
                  strMatch(); 
          //字符串匹配
              }


              
          private static void strMatch() {
                  String phone 
          = "13539770000";
                  
          //檢查phone是否是合格的手機(jī)號(hào)(標(biāo)準(zhǔn):1開頭,第二位為3,5,8,后9位為任意數(shù)字)
                  System.out.println(phone + ":" + phone.matches("1[358][0-9]{9,9}")); //true    
                  
                  String str 
          = "abcd12345efghijklmn";
                  
          //檢查str中間是否包含12345
                  System.out.println(str + ":" + str.matches("\\w+12345\\w+")); //true
                  System.out.println(str + ":" + str.matches("\\w+123456\\w+")); //false
              }


              
          private static void strSplit() {
                  String str 
          = "asfasf.sdfsaf.sdfsdfas.asdfasfdasfd.wrqwrwqer.asfsafasf.safgfdgdsg";
                  String[] strs 
          = str.split("\\.");
                  
          for (String s : strs){
                      System.out.println(s);
                  }
                  
              }


              
          private static void getStrings() {
                  String str 
          = "rrwerqq84461376qqasfdasdfrrwerqq84461377qqasfdasdaa654645aafrrwerqq84461378qqasfdaa654646aaasdfrrwerqq84461379qqasfdasdfrrwerqq84461376qqasfdasdf";
                  Pattern p 
          = Pattern.compile("qq(.*?)qq");
                  Matcher m 
          = p.matcher(str);
                  ArrayList
          <String> strs = new ArrayList<String>();
                  
          while (m.find()) {
                      strs.add(m.group(
          1));            
                  }
           
                  
          for (String s : strs){
                      System.out.println(s);
                  }
                  
              }


              
          private static void replace() {
                  String str 
          = "asfas5fsaf5s4fs6af.sdaf.asf.wqre.qwr.fdsf.asf.asf.asf";
                  
          //將字符串中的.替換成_,因?yàn)?是特殊字符,所以要用\.表達(dá),又因?yàn)閈是特殊字符,所以要用\\.來(lái)表達(dá).
                  str = str.replaceAll("\\.""_");
                  System.out.println(str);        
              }

          }















          歡迎大家訪問(wèn)我的個(gè)人網(wǎng)站 萌萌的IT人

          posted on 2013-05-31 20:26 一堣而安 閱讀(3027) 評(píng)論(0)  編輯  收藏 所屬分類: java

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 资溪县| 阜宁县| 苍山县| 荆州市| 平塘县| 白水县| 揭西县| 醴陵市| 绥宁县| 安泽县| 泾川县| 定南县| 沙田区| 龙门县| 湾仔区| 平江县| 四川省| 乐都县| 蒙阴县| 万安县| 珠海市| 赤峰市| 五台县| 和林格尔县| 晴隆县| 韩城市| 山丹县| 独山县| 阳朔县| 望江县| 道真| 怀柔区| 曲水县| 开封县| 夏河县| 延津县| 延长县| 秦皇岛市| 洛川县| 公主岭市| 峨眉山市|