从 Java 请求中提取变量的正则表达式

Regex to extract a Variable from a Java Request

所以我有一个工具可以扫描 API 以查找更改。如果他发现有变化,他会得到一个像这样的字符串:

word=\don\u2019t\ item-id=\"1086\">\n        <span class=\

我想从 item-id 中提取号码,但是响应中有多个号码。

有什么可行的方法吗? (我也不知道这个数字是4位还是1-2位)

所以 Regex 应该搜索类似 "NUMBERS\" 的东西并打印它。 (对于 Java)

    String string = "{\"success\":1,\"data\":{\"html\":\"<div class=\\"inner\\">\n      <span class=\\"title js-title-eligible\\">Upgrade available<\/span>\n    <span class=\\"title js-title-warning\\"><strong>WARNING :<\/strong> You don\u2019t own a <span class=\\"js-from-ship\\"><\/span><\/span>\n        <p class=\\"explain js-title-eligible\\">Buy this upgrade and it will be applicable to your <span class=\\"js-from-ship\\"><\/span> from the My Hangar section.<\/p>\n    <p class=\\"explain js-title-warning\\">You can buy this upgrade but it will only be applicable on a <span class=\\"js-from-ship\\"><\/span>.<\/p>\n\n    <div class=\\"price\\"><strong class=\\"final-price\\">\u20ac5<span class='super'>.41 <span class='currency'>EUR<\/span><\/span><\/strong><div class=\\"taxes js-taxes\\">\n  <div class=\\"taxes-details trans-02s\\">\n    <div class=\\"arrow\\"><\/div>\n    Tax Included: <br \/>\n    <ul>\n        <li>VAT 19%<\/li>\n        <\/ul>\n  <\/div>\n<\/div><\/div>\n\n\n    <div>\n      <a href=\\"\/pledge\/Upgrades\/Mustang-Alpha-To-Aurora-LN-Upgrade\\" class=\\"add-to-cart holosmallbtn trans-03s js-add-to-cart-ship ty-js-add-to-cart\\" data-sku=\"1086\\">\n        <span class=\\"holosmallbtn-top abs-overlay trans-02s\\">BUY NOW<\/span>\n        <span class=\\"holosmallbtn-bottom abs-overlay trans-02s\\"><\/span>\n      <\/a>\n      <a href=\\"\/pledge\/Upgrades\/Mustang-Alpha-To-Aurora-LN-Upgrade\\" class=\\"more-details\\">View more details<\/a>\n    <\/div>\n    \n    <p class=\\"explain info\\">\n      Upgrades that you buy can be found in your <a href=\\"\/account\/pledges\\">Hangar section<\/a>.<br \/>\n      Click \\"Apply Upgrade\\" inside the Upgrade Pledge to pick where you want to apply it.\n    <\/p>\n  <\/div>\n\n\n\n\"},\"code\":\"OK\",\"msg\":\"OK\"}";

    String pattern="(?<=data-sku=)([\\]*\")(\d+)";

    Pattern p = Pattern.compile(pattern);

    Matcher matcher = p.matcher(string);

     while (matcher.find()) {
            System.out.print("Start index: " + matcher.start());
            System.out.println(" End index: " + matcher.end() + " ");
            System.out.println("number="+matcher.group(2));
        }

根据 看来您正在接收 JSON 结构

{
    ...
    "data":{
        "html":".. <a .. data-sku=\"XXX\"> ..",
        ...
    }
    ...
}

并且您对 data-sku 属性的值感兴趣。

在这种情况下,解析 JSON 并遍历它以获得 HTML 结构。您可以为此使用 org.json.JSONObject(或其他解析器,选择一个您喜欢的)

String response = "{\"success\":1,\"data\":{\"html\":\"<div class=\\"inner\\">\n      <span class=\\"title js-title-eligible\\">Upgrade available<\/span>\n    <span class=\\"title js-title-warning\\"><strong>WARNING :<\/strong> You don\u2019t own a <span class=\\"js-from-ship\\"><\/span><\/span>\n        <p class=\\"explain js-title-eligible\\">Buy this upgrade and it will be applicable to your <span class=\\"js-from-ship\\"><\/span> from the My Hangar section.<\/p>\n    <p class=\\"explain js-title-warning\\">You can buy this upgrade but it will only be applicable on a <span class=\\"js-from-ship\\"><\/span>.<\/p>\n\n    <div class=\\"price\\"><strong class=\\"final-price\\">\u20ac5<span class='super'>.41 <span class='currency'>EUR<\/span><\/span><\/strong><div class=\\"taxes js-taxes\\">\n  <div class=\\"taxes-details trans-02s\\">\n    <div class=\\"arrow\\"><\/div>\n    Tax Included: <br \/>\n    <ul>\n        <li>VAT 19%<\/li>\n        <\/ul>\n  <\/div>\n<\/div><\/div>\n\n\n    <div>\n      <a href=\\"\/pledge\/Upgrades\/Mustang-Alpha-To-Aurora-LN-Upgrade\\" class=\\"add-to-cart holosmallbtn trans-03s js-add-to-cart-ship ty-js-add-to-cart\\" data-sku=\\"1086\\">\n        <span class=\\"holosmallbtn-top abs-overlay trans-02s\\">BUY NOW<\/span>\n        <span class=\\"holosmallbtn-bottom abs-overlay trans-02s\\"><\/span>\n      <\/a>\n      <a href=\\"\/pledge\/Upgrades\/Mustang-Alpha-To-Aurora-LN-Upgrade\\" class=\\"more-details\\">View more details<\/a>\n    <\/div>\n    \n    <p class=\\"explain info\\">\n      Upgrades that you buy can be found in your <a href=\\"\/account\/pledges\\">Hangar section<\/a>.<br \/>\n      Click \\"Apply Upgrade\\" inside the Upgrade Pledge to pick where you want to apply it.\n    <\/p>\n  <\/div>\n\n\n\n\"},\"code\":\"OK\",\"msg\":\"OK\"}";

JSONObject jsonObject = new JSONObject(response);
String html = jsonObject.getJSONObject("data") //pick data:{...} object
                        .getString("html");    //from that object get value of html:"..."

现在你有 html 你可以用 HTML 解析器解析它(我正在使用 jsoup)

Document doc = Jsoup.parse(html);
String dataSku = doc.select("a[data-sku]") //get "a" element with "data-sku" attribute
                    .attr("data-sku");     //value of that attribute

输出:1086.