读取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;
    }

这个正则规则是可以自己替换的,但是测试后发现文本过大可能会遇到栈溢出的问题...最后使用的是(*.?)的匹配规则,解决当前的问题。

posted @ 2021-10-27 13:01  Leejk  阅读(184)  评论(0编辑  收藏  举报