<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    tinguo002

     

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

    java正則表達式 提取、替換

    事例1
    http://www.cnblogs.com/lihuiyy/archive/2012/10/08/2715138.html
    比如,現(xiàn)在有一個 endlist.txt 文本文件,內(nèi)容如下:
    1300102,北京市
    1300103,北京市
    1300104,北京市
    1300105,北京市
    1300106,北京市
    1300107,北京市
    1300108,北京市
    1300109,北京市
    1300110,北京市
    1300111,北京市
    1300112,北京市
    1300113,北京市
    1300114,北京市
    1300115,北京市
    1300116,北京市
    1300117,北京市
    1300118,北京市
    1300119,北京市
    七位數(shù)字代表手機號碼的前七位,后面的漢字表示號碼歸屬地。現(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{3})(\\d{4},[\u4e00-\u9fa5]*\\n)" 每個()中的內(nèi)容為一組,索引從 1 開始,0表示整個表達式。所以這個表達式分為兩組,第一組表示3個數(shù)字,第二組表示 4個數(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(); 
    //用正則表達式獲取指定字符串內(nèi)容中的指定內(nèi)容
            System.out.println("********************");
            replace(); 
    //用正則表達式替換字符串內(nèi)容
            System.out.println("********************");
            strSplit(); 
    //使用正則表達式切割字符串
            System.out.println("********************");
            strMatch(); 
    //字符串匹配
        }


        
    private static void strMatch() {
            String phone 
    = "13539770000";
            
    //檢查phone是否是合格的手機號(標準: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";
            
    //將字符串中的.替換成_,因為.是特殊字符,所以要用\.表達,又因為\是特殊字符,所以要用\\.來表達.
            str = str.replaceAll("\\.""_");
            System.out.println(str);        
        }

    }















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

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

    導航

    統(tǒng)計

    常用鏈接

    留言簿(1)

    隨筆分類

    隨筆檔案

    收藏夾

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 午夜亚洲福利在线老司机| 婷婷亚洲综合五月天小说 | 国产美女无遮挡免费视频网站| 老色鬼久久亚洲AV综合| 一级女人18毛片免费| 男女超爽视频免费播放| 久久久亚洲精品无码| 午夜爱爱免费视频| 国产一区二区免费视频| 亚洲精品无码中文久久字幕| 四虎永久在线观看免费网站网址| 亚洲五月激情综合图片区| 日本免费人成黄页在线观看视频 | 亚洲自偷自偷偷色无码中文| 6080午夜一级毛片免费看| 日日摸日日碰夜夜爽亚洲| 麻豆国产入口在线观看免费 | 久久午夜羞羞影院免费观看| 亚洲国产AV一区二区三区四区| 精品少妇人妻AV免费久久洗澡| 亚洲日韩一区二区一无码| 亚洲国产三级在线观看| 国产成人精品高清免费| 免费一级全黄少妇性色生活片 | 亚洲AV本道一区二区三区四区| 99精品在线免费观看| 特级aaaaaaaaa毛片免费视频| 亚洲午夜激情视频| 毛片免费观看网站| 日韩免费的视频在线观看香蕉| 亚洲无成人网77777| 亚洲精品无码mv在线观看网站| 13小箩利洗澡无码视频网站免费| 久久亚洲精品中文字幕无码| 免费一级毛片在线观看| 成人免费AA片在线观看| 中文字幕免费视频| 亚洲av中文无码字幕色不卡| 亚洲日本乱码一区二区在线二产线| 天天看免费高清影视| 亚洲网站免费观看|