java – Jsoup getElementsByAttributeValueMatching
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – Jsoup getElementsByAttributeValueMatching,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1512字,纯文字阅读大概需要3分钟。
内容图文
![java – Jsoup getElementsByAttributeValueMatching](/upload/InfoBanner/zyjiaocheng/800/878a3b03141748fbaf3924c9def4d44b.jpg)
[JSoup讨论页面建议我在这里问我的问题.]
所以,我不是正则表达式专家,但我想知道我从jsoup得到的结果
getElementsByAttributeValueMatching()方法.
如果我有一个html页面,其中包含(以及其他)以下链接:
<a href="/tweb/tiles/twr/EIDS_AT_20130108T134335/01/">Parent Directory</a>
<a href="1357681618315/">1357681618315/</a>
<a href="1357681649996/">1357681649996/</a>
我查询:
Elements dirs = baseDir.getElementsByAttributeValueMatching("href", Pattern.compile("[0-9]+/"));
希望只得到只有数字的2个链接(最后是一个斜线).
但是,我得到了所有3个链接.
我编写了一个快速测试程序,用3 href字符串检查java的Pattern Matcher对该正则表达式的响应,并且它只返回两个只有我期望的数字:
String a = "/tweb/tiles/twr/EIDS_AT_20130108T134335/01/";
String b = "1357681618315/";
String c = "1357681649996/";
Pattern p = Pattern.compile("[0-9]+/");
System.out.println("a:"+ p.matcher(a).matches());
System.out.println("b:"+ p.matcher(b).matches());
System.out.println("c:"+ p.matcher(c).matches());
收益:
一:假
B:真
C:属实
所以,我的问题是,我错过了什么?
谢谢,
莱纳斯
解决方法:
Jsoup使用Matcher#find(),而不是Matcher#matches().所以,你需要自己提供^和$.
Elements dirs = baseDir.getElementsByAttributeValueMatching(
"href", Pattern.compile("^[0-9]+/$"));
以下是解释差异的相关的javadoc摘录(强调我的):
find
…
Returns:
true
if, and only if, a subsequence of the input sequence matches this matcher’s pattern
matches
…
Returns:
true
if, and only if, the entire region sequence matches this matcher’s pattern
至于为什么Jsoup使用find()而不是matches(),这是一个你要问其创建者的问题.
内容总结
以上是互联网集市为您收集整理的java – Jsoup getElementsByAttributeValueMatching全部内容,希望文章能够帮你解决java – Jsoup getElementsByAttributeValueMatching所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。