1. 程式人生 > 其它 >讀取txt資料,使用正則表示式輸出到控制檯

讀取txt資料,使用正則表示式輸出到控制檯

昨天工作時需要在日誌檔案找到需要的報文,Windows下提取還是比較麻煩了,試了很多方法。
比如,我將日誌放在了notepad++中,然後全域性搜尋需要的關鍵字所在位置。
但是,它太大了,如果要一一提取出來去測試報文是否正確,實在是麻煩。
之後想到了用Java寫個io程式。從一個檔案讀資料,比如:

      public static String fileTest() {

        File file = new File("E:\\code\\demo1\\src\\test\\java\\xml.txt");
        BufferedReader reader = null;

        StringBuffer sb = new StringBuffer();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sb.append(tempStr);
            }
            reader.close();
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return sb.toString();
    }

如果提取需要的內容呢?於是想到了用正則表示式。
看下面這個demo:

   /**
     * 正則表示式匹配兩個指定字串中間的內容
     */
    public static List<String> getSubUtil(String soap, String rgex) {

        List<String> list = new ArrayList<String>();

        Pattern pattern = Pattern.compile(rgex);// 匹配的模式

        Matcher m = pattern.matcher(soap);

        while (m.find()) {

            int i = 1;

            list.add(m.group(i));
            i++;
        }
        return list;
    }

最終方案:

public class MyTest2 {

    public static String fileTest() {

        File file = new File("E:\\code\\demo1\\src\\test\\java\\xml.txt");
        BufferedReader reader = null;

        StringBuffer sb = new StringBuffer();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sb.append(tempStr);
            }
            reader.close();
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return sb.toString();
    }
    //測試
    //(\\s|\\n)*表示多個空白符、換行。匹配中間的所有(.|\\s|\n)*除換行符的任意字元、空白符和換行符。
    public static void main(String[] args) {
        String str = fileTest();
      
        String rgex = "(?<st><name>(.|\\\\s|\\n)*</name>)";

        System.out.println(getSubUtil(str, rgex));
    }

    /**
     * 正則表示式匹配兩個指定字串中間的內容
     */
    public static List<String> getSubUtil(String soap, String rgex) {

        List<String> list = new ArrayList<String>();

        Pattern pattern = Pattern.compile(rgex);// 匹配的模式

        Matcher m = pattern.matcher(soap);

        while (m.find()) {

            int i = 1;

            list.add(m.group(i));
            i++;
        }
        return list;
    }

這個正則規則是可以自己替換的,但是測試後發現文字過大可能會遇到棧溢位的問題...最後使用的是(*.?)的匹配規則,解決當前的問題。

你應當熱愛自由!
作者:Leejk,轉載請註明原文連結:https://www.cnblogs.com/leejk/p/15470149.html