减去java文本字符串 [英] subtract java text string
问题描述
我想从URL中提取引用.
I want to extract the reference from an URL.
例如,我的网址如下:
"https://www.amazon.es/Lenovo-YOGA-520-14IKB-Ordenador-convertible/dp/B071WBF4PZ/"
我只想获取参考零件,即 B071WBF4PZ
I want to get only the reference part, that is B071WBF4PZ
我还想从这个html元素中提取价格:
I also want to extract the price from this html element:
"<div id="cerberus-data-metrics" style="display: none;" data-asin="B078ZYX4R5" data-asin-price="1479.00" data-asin-shipping="0" data-asin-currency-code="EUR" data-substitute-count="0" data-device-type="WEB" data-display-code="Asin is not eligible because it has a retail offer" ></div>"
我只需要获取属性 data-asin-price 的值.
I need to get only the value of the attribute data-asin-price.
可以用indexOf',
substring or
split`来完成,但我不知道该怎么做.
It could be done with indexOf',
substringor
split` but I don't get how to do it.
推荐答案
- 参考部分:
代码:
String url = "https://www.amazon.es/Lenovo-YOGA-520-14IKB-Ordenador-convertible/dp/B071WBF4PZ/";
String[] parts = string.split("/");
// parts :
// [0] = "https:"
// [1] = ""
// [2] = "www.amazon.es"
// [3] = "Lenovo-YOGA-520-14IKB-Ordenador-convertible"
// [4] = "dp"
// [5] = "B071WBF4PZ"
// [6] = ""
String reference = parts[5]; // < this is the reference.
但是,我建议使用常规表达式/模式
并在访问[5]
- 价格部分:
使用 Jsoup ,您可以轻松解析html并提取诸如data-asin-price
的属性.在这种情况下,我不会使用正则表达式.但是,正则表达式不需要额外的库.
Using Jsoup you can easily parse html and extract properties like data-asin-price
. In this case I would not use Regular Expressions. However Regular expressions don't need extra libraries.
此正则表达式:
(?:data-asin-price=")(\w*.\w*)
将匹配data-asin-price="
之后的任何数字-因此匹配组1将是:1479.00
will match any number after data-asin-price="
- so the match group 1 will be: 1479.00
这篇关于减去java文本字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!