正则表达式 → 匹配JSON字符串中指定key的value
```package org.example.wenhao.zz;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 正则表达式 → 匹配JSON字符串中指定key的value
*
* 正则表达式匹配字段值
* 不包含空值
* (?<=(href=")) 表示 匹配以(href=")开头的字符串,并且捕获(存储)到分组中
* (?=(">)) 表示 匹配以(">)结尾的字符串,并且捕获(存储)到分组中
*
*此方法不能匹配:"fieldName":null,假如需要匹配null。
*String regex = "(?<=(\"" + fieldName + "\":))(?=(null))";
*/
public class Zhengze {
String jsondata = "{\"spec_version\":\"2.0\",\"type\":\"bundle\",\"id\":\"bundle--81810123-b298-40f6-a4e7-186efcd07670\",\"from\":\"sxf\",\"objects\":[{\"type\":\"identity\",\"id\":\"identity--8c6af861-7b20-41ef-9b59-6344fd872a8f\",\"created\":\"2016-08-08T15:50:10.983Z\",\"modified\":\"2016-08-08T15:50:10.983Z\",\"name\":\"Franistan Intelligence\",\"identity_class\":\"organisation\",\"contact_information\":\"asdfsdf@163.com\"},{\"type\":\"identity\",\"id\":\"identity--ddfe7140-2ba4-48e4-b19a-df069432103b\",\"created\":\"2016-08-08T15:50:10.983Z\",\"modified\":\"2016-08-08T15:50:10.983Z\",\"external_references\":[{\"source_name\":\"website\",\"url\":\"http://www.bpp.bn\"}],\"name\":\"Branistan Peoples Party\",\"identity_class\":\"organisation\"},{\"type\":\"campaign\",\"id\":\"campaign--e5268b6e-4931-42f1-b379-87f48eb41b1e\",\"created\":\"2016-08-08T15:50:10.983Z\",\"modified\":\"2016-08-08T15:50:10.983Z\",\"name\":\"Operation Bran Flakes\",\"description\":\"A concerted effort to insert false information into the BPP's web pages\",\"aliases\":[\"OBF\"],\"first_seen\":\"2016-01-08T12:50:40.123Z\",\"objective\":\"Hack www.bpp.bn\"},{\"type\":\"intrusion-set\",\"id\":\"intrusion-set--ed69450a-f067-4b51-9ba2-c4616b9a6713\",\"created\":\"2016-08-08T15:50:10.983Z\",\"modified\":\"2016-08-08T15:50:10.983Z\",\"name\":\"APT BPP\",\"description\":\"An advanced persistent threat that seeks to disrupt Branistan's election with multiple attacks\",\"first_seen\":\"2016-01-08T12:50:40.123Z\",\"resource_level\":\"government\",\"primary_motivation\":\"ideology\",\"goals\":[\"Influence the Branistan election\",\"Disrupt the BPP\"],\"secondary_motivations\":[\"dominance\"],\"aliases\":[\"Bran-teaser\"]}]}";
public staticList<String> getFieldListFromJsonStr(String jsonStr, String fieldName) {
List<String> fieldValues = new ArrayList<>();
String regex = "(?<=(\"" + fieldName + "\":\")).*?(?=(\"))";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(jsonStr);
while (matcher.find()) {
if (StringUtils.isNotEmpty(matcher.group().trim())) {
fieldValues.add(matcher.group().trim());
}
}
return fieldValues;
}
@Test
public void testGetFieldListFromJsonStr() {
String jsondata = "{\"spec_version\":\"2.0\",\"type\":\"bundle\",\"id\":\"bundle--81810123-b298-40f6-a4e7-186efcd07670\",\"from\":\"sxf\",\"objects\":[{\"type\":\"identity\",\"id\":\"identity--8c6af861-7b20-41ef-9b59-6344fd872a8f\",\"created\":\"2016-08-08T15:50:10.983Z\",\"modified\":\"2016-08-08T15:50:10.983Z\",\"name\":\"Franistan Intelligence\",\"identity_class\":\"organisation\",\"contact_information\":\"asdfsdf@163.com\"},{\"type\":\"identity\",\"id\":\"identity--ddfe7140-2ba4-48e4-b19a-df069432103b\",\"created\":\"2016-08-08T15:50:10.983Z\",\"modified\":\"2016-08-08T15:50:10.983Z\",\"external_references\":[{\"source_name\":\"website\",\"url\":\"http://www.bpp.bn\"}],\"name\":\"Branistan Peoples Party\",\"identity_class\":\"organisation\"},{\"type\":\"campaign\",\"id\":\"campaign--e5268b6e-4931-42f1-b379-87f48eb41b1e\",\"created\":\"2016-08-08T15:50:10.983Z\",\"modified\":\"2016-08-08T15:50:10.983Z\",\"name\":\"Operation Bran Flakes\",\"description\":\"A concerted effort to insert false information into the BPP's web pages\",\"aliases\":[\"OBF\"],\"first_seen\":\"2016-01-08T12:50:40.123Z\",\"objective\":\"Hack www.bpp.bn\"},{\"type\":\"intrusion-set\",\"id\":\"intrusion-set--ed69450a-f067-4b51-9ba2-c4616b9a6713\",\"created\":\"2016-08-08T15:50:10.983Z\",\"modified\":\"2016-08-08T15:50:10.983Z\",\"name\":\"APT BPP\",\"description\":\"An advanced persistent threat that seeks to disrupt Branistan's election with multiple attacks\",\"first_seen\":\"2016-01-08T12:50:40.123Z\",\"resource_level\":\"government\",\"primary_motivation\":\"ideology\",\"goals\":[\"Influence the Branistan election\",\"Disrupt the BPP\"],\"secondary_motivations\":[\"dominance\"],\"aliases\":[\"Bran-teaser\"]}]}";
// List<String> id1 = Zhengze.getFieldListFromJsonStr(jsondata, "spec_version");
// id1.forEach((key)->{
// System.out.println(key);
// });
// 使用正则表达式,把字符串中的id替换成id1
String regex = "(?<=(\"id\":\")).*?(?=(\"))";
String replacement = "id1";
String result = jsondata.replaceAll(regex, replacement);
System.out.println("result = " + result);
// 使用正则表达式,匹配JSON中所有的id
String regex1 = "(?<=(\"id\":\")).*?(?=(\"))";
Pattern pattern = Pattern.compile(regex1);
Matcher matcher = pattern.matcher(jsondata);
while (matcher.find()) {
if (StringUtils.isNotEmpty(matcher.group().trim())) {
System.out.println(matcher.group().trim());
}
}
}
public static void main(String[] args) {
String jsondata = "{\"spec_version\":\"2.0\",\"type\":\"bundle\",\"id\":\"bundle--81810123-b298-40f6-a4e7-186efcd07670\",\"from\":\"sxf\",\"objects\":[{\"type\":\"identity\",\"id\":\"identity--8c6af861-7b20-41ef-9b59-6344fd872a8f\",\"created\":\"2016-08-08T15:50:10.983Z\",\"modified\":\"2016-08-08T15:50:10.983Z\",\"name\":\"Franistan Intelligence\",\"identity_class\":\"organisation\",\"contact_information\":\"asdfsdf@163.com\"},{\"type\":\"identity\",\"id\":\"identity--ddfe7140-2ba4-48e4-b19a-df069432103b\",\"created\":\"2016-08-08T15:50:10.983Z\",\"modified\":\"2016-08-08T15:50:10.983Z\",\"external_references\":[{\"source_name\":\"website\",\"url\":\"http://www.bpp.bn\"}],\"name\":\"Branistan Peoples Party\",\"identity_class\":\"organisation\"},{\"type\":\"campaign\",\"id\":\"campaign--e5268b6e-4931-42f1-b379-87f48eb41b1e\",\"created\":\"2016-08-08T15:50:10.983Z\",\"modified\":\"2016-08-08T15:50:10.983Z\",\"name\":\"Operation Bran Flakes\",\"description\":\"A concerted effort to insert false information into the BPP's web pages\",\"aliases\":[\"OBF\"],\"first_seen\":\"2016-01-08T12:50:40.123Z\",\"objective\":\"Hack www.bpp.bn\"},{\"type\":\"intrusion-set\",\"id\":\"intrusion-set--ed69450a-f067-4b51-9ba2-c4616b9a6713\",\"created\":\"2016-08-08T15:50:10.983Z\",\"modified\":\"2016-08-08T15:50:10.983Z\",\"name\":\"APT BPP\",\"description\":\"An advanced persistent threat that seeks to disrupt Branistan's election with multiple attacks\",\"first_seen\":\"2016-01-08T12:50:40.123Z\",\"resource_level\":\"government\",\"primary_motivation\":\"ideology\",\"goals\":[\"Influence the Branistan election\",\"Disrupt the BPP\"],\"secondary_motivations\":[\"dominance\"],\"aliases\":[\"Bran-teaser\"]}]}";
Zhengze zhengze = new Zhengze();
// List<String> id = zhengze.getFieldListFromJsonStr(jsondata, "id");
// id.forEach((key)->{
// System.out.println(key);
// });
List<String> id1 = Zhengze.getFieldListFromJsonStr(jsondata, "spec_version");
id1.forEach((key)->{
System.out.println(key);
});
}
}
```
页:
[1]