首页 > 解决方案 > 无法抓取第一个捕获组 - Matcher find() 显示为空

问题描述

String response = "{\"phone\":[{\"firstName\":\"sandeepan\",\"lastName\":\"\"}]}";
System.out.println("response "+response);
        Pattern ps = Pattern.compile("/\\{\"\\.\\*\"firstName\":\"([a-z]*)\"\\.\\*/");
        Matcher m = ps.matcher(response);
        while(m.find()) {
            System.out.println("matched "+m.group());
        }
        System.out.println("After matching block");

输出:

response {"phone":[{"firstName":"sandeepan","lastName":""}]}
After matching block

正则表达式正在工作 - https://regex101.com/r/L2vZ9J/1/

为什么我们使用正则表达式而不是 json 解析:

这只是一个一次性的任务,所以我们需要快速完成——没有太多的实施时间。

标签: javaregex

解决方案


推荐阅读