\n ..", -6ren">
gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:28:25 25 4
gpt4 key购买 nike

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

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

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

有没有可行的方法? (我也不知道号码是4位还是1-2位)

因此正则表达式应该搜索类似“NUMBERS\”的内容并打印它。 (对于Java)

最佳答案

基于your comment看起来您正在接收 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

关于java - 从 Java 请求中提取变量的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47487683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com