Frank Marlow
Frank Marlow

Reputation: 3

Regex to extract a Variable from a Java Request

so I have a tool that scans a API for changes. If he found a change, he get a String like:

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

I want to extract the Number from item-id , however there are multiple Numbers in the response.

Is there a possible way to do so? (I also dont know if the Number will 4 digits or just 1-2)

so the Regex should search for something like "NUMBERS\" and print it. (for Java)

Upvotes: 0

Views: 93

Answers (2)

Sameera
Sameera

Reputation: 83

    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));
        }

Upvotes: 0

Pshemo
Pshemo

Reputation: 124275

Based on your comment it looks like you are receiving JSON structure

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

and you are interested in value of data-sku attribute.

In that case parse that JSON and traverse it to get HTML structure. You can use org.json.JSONObject for that (or other parser, pick one you like)

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:"..."

Now that you have html you can parse it with HTML parser (I am using 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

Output: 1086.

Upvotes: 1

Related Questions