一个正则表达式的匹配输出问题

bristolcc 2010-08-11 12:48:16
有如下的代码,可正常工作。但唯一缺点就是在打印时有一些垃圾信息。

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Test {

public static void main(String args[]){
try {
BufferedReader reader = new BufferedReader(new FileReader("Web/a1.html"));

String line = "";
try {
while((line = reader.readLine())!=null){
pare(line);
}
} catch (IOException e) {
System.out.println("cannot read file");
}

} catch (FileNotFoundException e) {
System.out.println("cannot find file");
}
}

private static void pare(String line) {
Pattern p = Pattern.compile("<a\\s+href\\s*=\\s*\"?(.*?)[\"|>]");
Matcher m = p.matcher(line);

while(m.find()){
System.out.println(m.group());
}

}
}
打印出:
<a href="a2.html"
<a href="a3.html"
<a href="a4.html"
<a href="a5.html"

我只想打印出
a2.html
a3.html
a4.html
a5.html

请问正则应该如何修改呢??
...全文
69 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
bristolcc 2010-08-11
  • 打赏
  • 举报
回复
谢谢!!
jays1235 2010-08-11
  • 打赏
  • 举报
回复
System.out.println(m.group(1));
huntor 2010-08-11
  • 打赏
  • 举报
回复
System.out.println(m.group(1));

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧