吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 207|回复: 0
收起左侧

[学习记录] 正则表达式 → 匹配JSON字符串中指定key的value

[复制链接]
wh52pj 发表于 2023-12-8 22:29
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 static  List<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);
        });

    }

}

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-12 09:17

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表