gpt4 book ai didi

Java正则表达式热匹配元标签内容属性值

转载 作者:行者123 更新时间:2023-12-01 18:36:39 28 4
gpt4 key购买 nike

我有一个正则表达式,希望它匹配 html 元标记内容属性并获取其内容。例如:

<meta name="description" content="Some website description.">

本例中得到

Some website description.

仅此而已。就我而言,我使用这种模式:

private static Pattern siteMetaTagDescriptionAttributePattern = Pattern.compile("name=\"description\"(\\s*)content=\"(.*)\"");
Matcher matcher = siteMetaTagDescriptionAttributePattern.matcher(siteContentLine);
String siteDescription = "";
while(matcher.find()) {
siteDescription = matcher.group(2);
}

直到行尾,在本例中是这样的:

Some website description.">

在这种情况下,我应该怎么做才能只获取内容属性的内部内容

Some website description.

非常感谢。

最佳答案

考虑使用解析器而不是正则表达式。例如,您可以使用 Jsoup喜欢

String html = "<meta name=\"description\" content=\"Some website description.\">";

Document doc =Jsoup.parse(html);
System.out.println(doc.select("meta[name=description]").attr("content"));

输出:

Some website description.

关于Java正则表达式热匹配元标签内容属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21686984/

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