<div class="col-12 col-md-6 col-xl-4 provider hide">
<a rel="nofollow" href="/company/zade-servers/?go=true&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&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&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 友帮忙看看
1
mgcnrx11 2020-03-12 21:43:52 +08:00 via iPhone
jsoup 解析后读出
|
2
wujieyuan 2020-03-12 21:48:44 +08:00
<a href=\"(.*?)\" class
|
3
EminemW 2020-03-12 21:49:05 +08:00
jsoup
|
4
goyiyi OP |
5
heyjei 2020-03-12 22:06:32 +08:00
^ 表示前面没有字符,,你的 html 中 空格也是字符啊
|
6
HuHui 2020-03-12 22:10:27 +08:00 via Android
好好看看文档
|
8
guolaopi 2020-03-12 22:45:57 +08:00
明显 a 后面有 rel 你没匹配啊。。。
|
10
guolaopi 2020-03-12 23:34:26 +08:00
|
11
aguesuka 2020-03-13 00:14:45 +08:00 via Android
html 本质是 xml,用 xpath 语法比较好吧
|
12
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 匹配多个的话,不能用贪婪模式 |
13
T0DD 2020-03-13 00:44:19 +08:00
@goyiyi 另外,我自己测试过对于较长的 html 字符串(>2000 ),用 regex 做多匹配性能比较差,差不多一次计算要 20 ~ 25ms
可以尝试用 String.indexOf & String.subString 配合,写一个固定场景下的 extractStrings(), 能有 20 倍左右的性能提升。 |
14
zifangsky 2020-03-13 09:11:18 +08:00
用 XPath 解析 +1
|
15
lu5je0 2020-03-13 10:13:20 +08:00
href="(.*?/)"
|
17
lithium4010 2020-03-13 14:36:42 +08:00
xpath
|
18
lithium4010 2020-03-13 14:37:53 +08:00
jsoup
|