subtract java text string

蓝咒 提交于 2019-12-10 11:54:33

问题


I want to extract the reference from an URL.

For example, my URL looks like:

"https://www.amazon.es/Lenovo-YOGA-520-14IKB-Ordenador-convertible/dp/B071WBF4PZ/"

I want to get only the reference part, that is B071WBF4PZ

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

I need to get only the value of the attribute data-asin-price.

It could be done with indexOf',substringorsplit` but I don't get how to do it.


回答1:


  • Reference part:

code:

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.

However, I would recommend using Regular Expressions / patterns

And also check if parts.length() >= 6 before you access [5]

  • Price part:

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.

This RegEx:

(?:data-asin-price=")(\w*.\w*)

will match any number after data-asin-price=" - so the match group 1 will be: 1479.00



来源:https://stackoverflow.com/questions/50016760/subtract-java-text-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!