java - 无法抓取第一个捕获组 - 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 解析:
- 针对给定电话号码的响应中可能有多组数据,我们可以抓住第一组数据。
- 第一个属性中有一个值(“phone”是一个值而不是一个键,所以需要将它建模为一个看起来像的类,才能正确地做到这一点。)
这只是一个一次性的任务,所以我们需要快速完成——没有太多的实施时间。
解决方案
推荐阅读
- java - Intellij Idea 提示“Unknown type Long” Grapqhl 的配置
- mysql - Mysql FIFO消耗
- msbuild - 错误 NU1101:找不到包 ProjectABC.Core.Services。源中不存在具有此 ID 的包
- python - 将 Pandas 数据框中的 JSON 数据转换为数据框本身的最佳方法
- python - 如何在 Python 3 中将列表变成循环列表?
- mysql - 根据 id 列将 MySQL 表从垂直转换为水平
- css - Less - 如何在循环中生成值
- python - gensim doc2vec 模型不学习一些单词
- angular - 如何在 Ionic4 应用程序的 ngInit 中执行多个嵌套的 firebase 调用
- python - 根据不同的表格更改文本文件中的变量