我有一个模式来匹配类似的
...
<span class="count">1036</span>
...但我不想和
<span class="count">1036</span></span>因为它会抓住
1036</span>但无论如何,我不想抓住双跨度,因为我不需要这些数据。我需要一个跨度和线尾之间的数据。
我试过了\n在跨度的尽头,但它不起作用.下面是模式:
private static final Pattern COUNT = Pattern.compile("<span class=\"count\">(.+?)</span> ");谢谢你的回答
发布于 2014-06-10 14:10:37
尝试使用包含在括号()中的regex的分组特性,并使用Matcher#group(1)获取它。
Regex模式
<span class="count">([^<]*?)</span>样本代码:
Pattern pattern = Pattern.compile("<span class=\"count\">([^<]*?)</span>");
Matcher matcher = pattern.matcher("<span class=\"count\">1036</span></span>");
while (matcher.find()) {
System.out.println(matcher.group(1));
}产出:
1036发布于 2014-06-10 13:58:45
“行尾”的regex代码是$。
尝试:
private static final Pattern COUNT = Pattern.compile("<span class=\"count\">(.+?)</span>$ ");发布于 2014-06-10 14:02:21
使用多行开关(?m),使^和$匹配开始/结束行.
Pattern COUNT = Pattern.compile("(?m)<span class=\"count\">(.+?)</span>$");https://stackoverflow.com/questions/24142890
复制相似问题