JAVA 正则匹配 反斜杠

发布时间 2023-03-27 21:13:31作者: chuangzhou

原始数据:

"attrs": "{\"deliveryTime\":120,\"discountValue\":1,\"serialName\":\"平键 普通型/淬火型\",\"productModelNumber\":\"AJPA-C2-HA-W4-L8\",\"goodsPriceTaxIncluded\":60.000000,\"discountPriceTaxIncluded\":60.000000,\"subtotalTaxIncluded\":60.00,\"serialAccessId\":\"23d7cf97d375481b973b14deb2696bba\",\"fileAccessId\":\"https://test.jlcfa.com/api/faMall/fileInfo/downloadFile?fileAccessId=aliFaSerialSelf_21ffb42ddf5f402eaf9b2c0f9aae3778\",\"publicReadFilePath\":\"Serial/A01/AJPA/158507650437750786.jpg\",\"productCodeKeyId\":842,\"weight\":20.00,\"type\":\"order\",\"delivery\":1680537599000,\"productCodeAccessId\":\"8b1f0b83a02b498288fef1ff71d7a0a8\",\"goodsInStock\":0,\"orderGoodsDeliveryTime\":120,\"orderGoodsDelivery\":1680537599000,\"materialCode\":\"Y22010001\",\"bottom\":false,\"promotionAccessId\":\"104b34cb69aa4b75bed4edcb3afa1625\",\"promotionSkuInfoAccessId\":\"0482be95439240b5bc0e06ada5f5315d\",\"promotionGoodsFlag\":true,\"couponAllowed\":true,\"productCode\":\"AJPA-C2-HA\",\"serialCode\":\"AJPA\",\"parentCategoryCode\":\"A\",\"lastCategoryCode\":\"A01\"}",
"cartGoodId": null,


# 想匹配:delivery\":1680537599000,\"productCodeAccessId\" 中的 1680537599000
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Demo5 {
    /*
    1.JAVA中两个\\代表一个反斜杠
    2.想要匹配一个\ 因此需要使用\\\\,前两个\\转义后两个\\
    */
    public static void main(String[] args) {


        String ss = "delivery\\\":1680537599000,\\\"productCodeAccessId\\\":\\\"8b1f0b83a02b498288fef1ff71d7a0a8\\\"";

        Pattern p = Pattern.compile("delivery\\\\\":(.*?),\\\\\"productCodeAccessId");    //用了五个反斜杠,最后一个:转义双引号,

        Matcher matcher = p.matcher(s);

        while (matcher.find()) {
            String group = matcher.group(1);
            System.out.println(group);  //out : 1680537599000
        }
    }

}