正则问题。

w1w1w1w 2004-06-26 05:56:00
我想替换HTML源文件的所有超链,假定目标链接已经确定,那么提取源链接的正则该怎么写,先谢拉!
比如:
String s=" <link href=\"../css/webstyle.css\" rel=\"stylesheet\" type=\"text/css\"> ";

s+=" dgdfgfdghfd ";

s+="<img SRC='../css/webstyle.css' > ";

我该怎么弄到s中的源链呢?
...全文
106 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
79cy 2004-06-28
  • 打赏
  • 举报
回复
instr查找还是没有正则表达式方便快捷,楼主的方法已经不错了。
nwsl 2004-06-27
  • 打赏
  • 举报
回复
各位高手可否讲得更清楚一点?
nwsl 2004-06-27
  • 打赏
  • 举报
回复
不明白。
CoolAbu 2004-06-27
  • 打赏
  • 举报
回复
用instr查找也可以的。
w1w1w1w 2004-06-27
  • 打赏
  • 举报
回复
汗,自己搞定了
s = "<img src='/css/wtyle.css" type=\"text/css\"> <link href=\"../css/webstyle.css\" rel=\"stylesheet\" type=\"text/css\"> ";
s += " dgdfgfdghfd <img src='\" type=\"text/css\">";
s += "<img SRC='/css/webstyle.css'> ";
String regEx = "(href|src)=\\s*(\"|')([^\"|^']+)\\s*(\"|')";
Pattern p = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(s);
while (m.find()) {
try {
s = s.replaceFirst(m.group(3).replaceAll("\\?","\\\\\\?"), "http://");
}
catch (Exception e) {
System.err.println(e.getMessage());
}
}
System.out.println(s);


有兴趣的可以自己完善一下。




谁来接分!!!

62,614

社区成员

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

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