Java 正则表达式的捕获组用于过滤日志拿到有效的信息

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/chenaini119/article/details/88092626

普通捕获组
从正则表达式左侧开始,每出现一个左括号"("记做一个分组,分组编号从 1 开始。0 代表整个表达式。

对于时间字符串:2017-04-25,表达式如下

(\d{4})-((\d{2})-(\d{2}))

public static final String DATE_STRING = "2017-04-25";
public static final String P_COMM = "(\\d{4})-((\\d{2})-(\\d{2}))";
 
Pattern pattern = Pattern.compile(P_COMM);
Matcher matcher = pattern.matcher(DATE_STRING);
matcher.find();//必须要有这句
System.out.printf("\nmatcher.group(0) value:%s", matcher.group(0));
System.out.printf("\nmatcher.group(1) value:%s", matcher.group(1));
System.out.printf("\nmatcher.group(2) value:%s", matcher.group(2));
System.out.printf("\nmatcher.group(3) value:%s", matcher.group(3));
System.out.printf("\nmatcher.group(4) value:%s", matcher.group(4));

实际效果展示


public class Help{
    public static final String DATE_STRING = "wo ai wdf chad.chen  参数{\"picId\":628308388,\"goodsId\":null,\";
    public static final String P_COMM = "picId\\\":(\\d+),.*";

    public static void main(String[] args) {
        readFile();
    }


    private static void readFile() {
        try {
            Set<String> mallIds = new HashSet<>();
            List<File> fileList = new ArrayList<File>();
            File file = new File("C:\\Users\\chad.chen\\Desktop\\logs\\logs");
            File[] files = file.listFiles();// 获取目录下的所有文件或文件夹
            if (files == null) {// 如果目录为空,直接退出
                return;
            }
            // 遍历,目录下的所有文件
            for (File f : files) {
                if (f.isFile()) {
                    fileList.add(f);
                }
            }
            for (File f1 : fileList) {
                System.out.println(f1.getName());
                //解析所有的文件
                InputStreamReader read = new InputStreamReader(new FileInputStream(f1));
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    System.out.println(lineTxt);
                    //解析txt
                    Pattern pattern = Pattern.compile(P_COMM);
                    Matcher matcher = pattern.matcher(lineTxt);
                    matcher.find();//必须要有这句
                    try {
                        System.out.printf("matcher.group(1) value:%s", matcher.group(1));
                        mallIds.add(matcher.group(1));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                read.close();
            }
            System.out.println(JsonUtils.toJson(mallIds));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public class Help{
   /* public static final String DATE_STRING = "repair check good very : {\"id\":12241515853,\"good\":583484268,\"chanen\":null";
    public static final String P_COMM = "id\\\":(\\d+),\\\"good\\\":(\\d+),.*";

    public static void main(String[] args) {
        readFile();
    }
    private static void readFile() {
        try {
            List<ChenBean> chenBeans = new ArrayList<>();
            List<File> fileList = new ArrayList<File>();
            File file = new File("C:\\Users\\chad.chen\\Desktop\\logs");
            File[] files = file.listFiles();// 获取目录下的所有文件或文件夹
            if (files == null) {// 如果目录为空,直接退出
                return;
            }
            // 遍历,目录下的所有文件
            for (File f : files) {
                if (f.isFile()) {
                    fileList.add(f);
                }
            }
            for (File f1 : fileList) {
                //解析所有的文件
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(f1));//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    System.out.println(lineTxt);
                    //解析txt
                    Pattern pattern = Pattern.compile(P_COMM);
                    Matcher matcher = pattern.matcher(lineTxt);
                    matcher.find();//必须要有这句
                    try {
                        System.out.printf("matcher.group(1) value:%s", matcher.group(1));
                        System.out.printf("matcher.group(2) value:%s", matcher.group(2));
                        ChenBean chenBean= new ChenBean();
                        chenBean.setId(Long.valueOf(matcher.group(1)));
                        chenBean.setGood(Long.valueOf(matcher.group(2)));
                        dataBeans.add(dataBean);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                read.close();
            }
            System.out.println(JsonUtils.toJson(dataBeans));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static class ChenBean{
        private Long good;
        private Long id;
        public ChenBean() {
            super();
        }
        public Long getId() {
            return id;
        }
        public void setId(Long id) {
            this.id= id;
        }
        public Long getGood() {
            return good;
        }
        public void setGood(Long good) {
            this.good= good;
        }
    }*/
}

这样就可以把你想要的提取出来了,不管多少文件,修复数据很管用。有帮助到你的话,可以点赞。

展开阅读全文

没有更多推荐了,返回首页