[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 个链接。
我编写了一个快速测试程序来检查 java 的模式匹配器对带有 3 个 href 字符串的正则表达式的响应,并且它只返回两个只有数字的,正如我所期望的:
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());
返回:a:false b:true c:true
所以,我的问题是,我错过了什么?
谢谢,莱纳斯