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

本文深入探讨了正则表达式的普通捕获组概念,通过实例详细讲解了如何使用Java的Pattern和Matcher类来匹配和提取特定的日期格式和参数值。展示了如何解析多个文件,提取所需的数据,并将其转换为特定的Java对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

普通捕获组
从正则表达式左侧开始,每出现一个左括号"("记做一个分组,分组编号从 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;
        }
    }*/
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值