Java 如何匹配 html 文本

2020-03-12 21:34:59 +08:00
 goyiyi
<div class="col-12 col-md-6 col-xl-4 provider hide"> 
          	<a rel="nofollow" href="/company/zade-servers/?go=true&amp;hidden=true" class="dont-style-link"> Don't visit this </a> 
         </div> 
         <div class="col-12 col-md-6 col-xl-4 provider" data-filter="zadeservers"> 
          	<a href="/company/zade-servers/" class="dont-style-link"> </a> 
         </div> 
         <div class="col-12 col-md-6 col-xl-4 provider hide"> 
          	<a rel="nofollow" href="/company/zappie-host-llc/?go=true&amp;hidden=true" class="dont-style-link"> Don't visit this </a> 
         </div> 
         <div class="col-12 col-md-6 col-xl-4 provider" data-filter="zappiehostllc"> 
          	<a href="/company/zappie-host-llc/" class="dont-style-link">  </a> 
         </div> 
         <div class="col-12 col-md-6 col-xl-4 provider hide"> 
          	<a rel="nofollow" href="/company/zare/?go=true&amp;hidden=true" class="dont-style-link"> Don't visit this </a> 
         </div> 
         <div class="col-12 col-md-6 col-xl-4 provider" data-filter="zare"> 
          	<a href="/company/zare/" class="dont-style-link">  </a> 
         </div> 
         
         更多省略了 ...  ... 

java 如何匹配其中的 /company/zade-servers/ /company/zappie-host-llc/ /company/zare/ 字符串,然后把他们装进 list<string></string>

我写的规则:"^<a href="\/company\/.*\/$"

结果:无法匹配出内容,希望懂正则的 v 友帮忙看看

2118 次点击
所在节点    程序员
18 条回复
mgcnrx11
2020-03-12 21:43:52 +08:00
jsoup 解析后读出
wujieyuan
2020-03-12 21:48:44 +08:00
<a href=\"(.*?)\" class
EminemW
2020-03-12 21:49:05 +08:00
jsoup
goyiyi
2020-03-12 21:58:11 +08:00
@mgcnrx11
@EminemW jsoup 的 Document 好像只能拿到节点 多个 Element,还是需要去匹配拿 href 里的内容,主要是拿超链接节点的时候,还存在干扰项,我不知道该怎么排除它:<a rel="nofollow noopener" href="/company/zade-servers/?go=true&amp;hidden=true" class="dont-style-link"> Don't visit this </a>
heyjei
2020-03-12 22:06:32 +08:00
^ 表示前面没有字符,,你的 html 中 空格也是字符啊
HuHui
2020-03-12 22:10:27 +08:00
好好看看文档
goyiyi
2020-03-12 22:41:35 +08:00
@HuHui 话题终结者,你为什么要回复我的帖子,哈哈
guolaopi
2020-03-12 22:45:57 +08:00
明显 a 后面有 rel 你没匹配啊。。。
goyiyi
2020-03-12 22:48:04 +08:00
@guolaopi 那个是干扰项,不要的
guolaopi
2020-03-12 23:34:26 +08:00
@goyiyi
这么说吧,你写这个是匹配 a 空格 href 这样的字符串,但是你给的例子里都是 a 空格 rel=xxx 空格 href
所以根本匹配不到啊。。。
aguesuka
2020-03-13 00:14:45 +08:00
html 本质是 xml,用 xpath 语法比较好吧
T0DD
2020-03-13 00:39:15 +08:00
String reg = "a href=\"/company/.+?/";
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(a); // a is your original string.
List<String> resultList = new LinkedList<>();
while (m.find()) {
String str = m.group();
str = "<string>" + str.replace("a href=\"", "") + "</string>";
resultList.add(str);
}


output:
[<string>/company/zade-servers/</string>,
<string>/company/zappie-host-llc/</string>,
<string>/company/zare/</string>]


@goyiyi 匹配多个的话,不能用贪婪模式
T0DD
2020-03-13 00:44:19 +08:00
@goyiyi 另外,我自己测试过对于较长的 html 字符串(>2000 ),用 regex 做多匹配性能比较差,差不多一次计算要 20 ~ 25ms
可以尝试用 String.indexOf & String.subString 配合,写一个固定场景下的 extractStrings(), 能有 20 倍左右的性能提升。
zifangsky
2020-03-13 09:11:18 +08:00
用 XPath 解析 +1
lu5je0
2020-03-13 10:13:20 +08:00
href="(.*?/)"
goyiyi
2020-03-13 12:59:18 +08:00
@T0DD useful
lithium4010
2020-03-13 14:36:42 +08:00
xpath
lithium4010
2020-03-13 14:37:53 +08:00
jsoup

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/652312

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX