gpt4 book ai didi

java - 匹配java Regex中特定html标签的选定选项

转载 作者:搜寻专家 更新时间:2023-11-01 01:32:48 26 4
gpt4 key购买 nike

我必须解析一些 html 以从一些 HTML 中找到一组值,这些值并不总是格式正确并且我无法控制(因此 Scanner 似乎不是一个选项)

这是一个购物车,购物车内有 n 行,每行包含一个数量下拉列表。现在我希望能够获得购物车中产品的总和。

鉴于此 html,我想匹配值 2 和 5

...
<select attr="other stuff" name="quantity">
<option value="1" />
<option value="2" selected="selected" />
</select>
....
<select name="quantity" attr="other stuff">
<option selected="selected" value="5" />
<option value="6" />
</select>

我做了很多可怜的尝试,但考虑到变量的数量(例如“值”和“已选择”标签的顺序),我的大多数解决方案要么不起作用,要么非常慢。

我结束的最后一个 Java 代码如下

Pattern pattern = Pattern.compile("select(.*?)name=\"quantity\"([.|\\n|\\r]*?)option(.*?)value=\"(/d)\" selected=\"selected\"", Pattern.DOTALL);
Matcher matcher = pattern.matcher(html);
if (matcher.find()) {
....
}

它非常慢,并且在属性顺序更改时不起作用。我的 Regex 知识不足以编写有效的模式

最佳答案

您可以使用 XPath 表达式来检索您在问题中拥有的 HTML 的所有值属性,而不是使用正则表达式:

//select[@name="quantity"]/option[@selected="selected"]/@value

一句话:

  • 查找所有 <select> XML 中具有属性 name 的元素等于 quantity , 带有一个子元素 <option>具有属性 selected等于 selected
  • 检索 value属性。

我真的会考虑尝试使用 XQuery/XPath,这就是它的用途。阅读this answer问题How to read XML using XPath in Java关于如何检索值。 XPath 表达式介绍 here .


考虑这样一种情况,在这种情况下,您以后只需要找到属性为 selected="selected" 的选项。 例如status="accepted" . XPath 表达式将简单地变为:

//select[@name="quantity"]/option[@selected="selected" and @status="accepted"]/@value

XPath 表达式易于扩展、易于审查、易于证明其作用。

现在您必须为添加的条件创建什么样的 RegEx 怪物?写起来难,维护起来更难。代码审阅者如何判断复杂的(cf bobble bubble's answer)正则表达式在做什么?你如何证明正则表达式确实在做它应该做的事情?

您当然可以记录正则表达式,这是您应该始终为正则表达式做的事情。但这并不能证明什么。

我的建议:除非万不得已,否则请远离正则表达式。


对于运动,我做了一个片段来展示这种工作方式的基础:

import java.io.StringReader;
import javax.xml.xpath.*;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class ReadElementsFromHtmlUsingXPath {
private static final String html=
"<html>Read more about XPath <a href=\"www.w3schools.com/xsl/xpath_intro.asp\">here</a>..."+
"<select attr=\"other stuff\" name=\"quantity\">"+
"<option value=\"1\" />"+
"<option value=\"2\" selected=\"selected\" />"+
"</select>"+
"<i><b>Oh and here's the second element</b></i>"+
"<select name=\"quantity\" attr=\"other stuff\">"+
"<option selected=\"selected\" value=\"5\" />"+
"<option value=\"6\" />"+
"</select>"+
"And that's all folks</html>";

private static final String xpathExpr =
"//select[@name=\"quantity\"]/option[@selected=\"selected\"]/@value";

public static void main(String[] args) {
try {
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile(xpathExpr);
NodeList nodeList = (NodeList) expr.evaluate(new InputSource(new StringReader(html)),XPathConstants.NODESET);
for( int i = 0; i != nodeList.getLength(); ++i )
System.out.println(nodeList.item(i).getNodeValue());
} catch (XPathExpressionException e) {
e.printStackTrace();
}
}
}

输出结果:

2
5

关于java - 匹配java Regex中特定html标签的选定选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34086941/

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