【问题标题】:how can i split this string which is get from a html document with using jsoup at java? [duplicate]如何使用 java 中的 jsoup 拆分从 html 文档中获取的字符串? [复制]
【发布时间】:2012-07-02 19:27:41
【问题描述】:

可能重复:
How do I split a string with any whitespace chars as delimiters?

<div class="listPrice" style="font-size: 12px; padding: 10px 0 0 0;"> HSN Price:&nbsp;$9.95 </div>

您好,我正在尝试将其与其空格分开,但“ ”给我带来了一些问题。我正在使用 jsoup 在 java 编写代码。我发送element.text().split(" "); 但这个或element.text().split("&amp;nbsp;"); 不能在“”执行 你能帮我解决一下这种情况吗?

【问题讨论】:

  • 是空白的好解决方案,但我需要为 html 元素执行此操作。
  • 你想提取什么信息?
  • 你能告诉我们更多为什么 split(" ") 对你不利,因为" " is making some problem for me 有点模糊
  • 我只需要提取价格值并尝试拆分该文本,但它带有“ ”所以我问这个值有一个特殊的字符名称或用于拆分的东西吗?所以我只需要 9.95 美元
  • @Pshemo 我刚遇到这个问题,我的 element.text() 值是“HSN Price: $199.00”,但我不能使用 str.split(" ");

标签: java html jsoup


【解决方案1】:

如果价格是你想要的,你可以试试类似的东西

Matcher matcher = Pattern.compile("(\\$.*)\\s*</div>").matcher(element.text());
String price = null;
if (matcher.find()) {
  price = matcher.group(1);
}

【讨论】:

  • 忘记转义 $.现在可以了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 2014-01-18
相关资源
最近更新 更多