正则表达式问题--在线等待

一峰还有一峰小 2003-09-16 03:33:50
我使用java的regex,想获得web页面上所有的链接,我的模式串为:

<a(\\s|\r|\n)+href[\\s]*=[\\s]*('|\")?[^'\"\\s>]+('|\")?(\\s|>|#)

但是有问题,谁能给我帮助?谢谢!
...全文
16 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
scbb 2003-09-16
  • 打赏
  • 举报
回复
java.util.regex.Pattern p = java.util.regex.Pattern.compile("<[Aa][\\s]*(href|HREF)[\\s]*=[\\s]*['\"].+['\"].*>");
RobertDeNiro 2003-09-16
  • 打赏
  • 举报
回复
如果你是想匹配<a href=http>**</a>的话,我看不出你的模式串的作用,其实匹配html中的<a href=http>**</a>连接的话试试<\\s*a\\s+href\\s*=\\s*.*\\s*>
  • 打赏
  • 举报
回复
我快要昏倒啦!

如何写一个模式串匹配web页面上的链接?
toplchx 2003-09-16
  • 打赏
  • 举报
回复
你在哪里用的这个regex?
ponky 2003-09-16
  • 打赏
  • 举报
回复
不知道。来看看
  • 打赏
  • 举报
回复
用法我已经会了,我是说,我的模式串有时不能找到所有链接,谁能

给我一个模式串?

谢谢!
mtou 2003-09-16
  • 打赏
  • 举报
回复
jdk1.4中加入了java.util.regex包提供对正则表达式的支持。而且Java.lang.String类中的replaceAll和split函数也是调用的正则表达式来实现的。

正则表达式对字符串的操作主要包括:字符串匹配,指定字符串替换,指定字符串查找和字符串分割。下面就用一个例子来说明这些操作是如何实现的:

<%@ page import="java.util.regex.*"%>

<%

Pattern p=null; //正则表达式

Matcher m=null; //操作的字符串

boolean b;

String s=null;

StringBuffer sb=null;

int i=0;

//字符串匹配,这是不符合的

p = Pattern.compile("a*b");

m = p.matcher("baaaaab");

b = m.matches();

out.println(b+"<br>");

//字符串匹配,这是符合的

p = Pattern.compile("a*b");

m = p.matcher("aaaaab");

b = m.matches();

out.println(b+"<br>");

//字符串替换

p = Pattern.compile("ab");

m = p.matcher("aaaaab");

s = m.replaceAll("d");

out.println(s+"<br>");

p = Pattern.compile("a*b");

m = p.matcher("aaaaab");

s = m.replaceAll("d");

out.println(s+"<br>");

p = Pattern.compile("a*b");

m = p.matcher("caaaaab");

s = m.replaceAll("d");

out.println(s+"<br>");

//字符串查找

p = Pattern.compile("cat");

m = p.matcher("one cat two cats in the yard");

sb = new StringBuffer();

while (m.find()) {

m.appendReplacement(sb, "dog");

i++;

}

m.appendTail(sb);

out.println(sb.toString()+"<br>");

out.println(i+"<br>");

i=0;

p = Pattern.compile("cat");

m = p.matcher("one cat two ca tsi nthe yard");

sb = new StringBuffer();

while (m.find()) {

m.appendReplacement(sb, "dog");

i++;

}

m.appendTail(sb);

out.println(sb.toString()+"<br>");

out.println(i+"<br>");





p = Pattern.compile("cat");

m = p.matcher("one cat two cats in the yard");

p=m.pattern();

m = p.matcher("bacatab");

b = m.matches();

out.println(b+"<br>");

s = m.replaceAll("dog");

out.println(s+"<br>");



i=0;

p = Pattern.compile("(fds){2,}");

m = p.matcher("dsa da fdsfds aaafdsafds aaf");

sb = new StringBuffer();

while (m.find()) {

m.appendReplacement(sb, "dog");

i++;

}

m.appendTail(sb);

out.println(sb.toString()+"<br>");

out.println(i+"<br>");



p = Pattern.compile("cat");

m = p.matcher("one cat two cats in the yard");

sb = new StringBuffer();

while (m.find()) {

m.appendReplacement(sb, "<font color=\"red\">cat</font>");

}

m.appendTail(sb);

out.println(sb.toString()+"<br>");

String aa=sb.toString();

out.println(aa+"<br>");

//字符串分割

p = Pattern.compile("a+");

String[] a=p.split("caaaaaat");

for(i=0;i<a.length;i++)

{

out.println(a[i]+"<br>");

}

p = Pattern.compile("a+");

a=p.split("c aa aaaa t",0);

for(i=0;i<a.length;i++)

{

out.println(a[i]+"<br>");

}

p = Pattern.compile(" +");

a=p.split("c aa aaaa t",0);

for(i=0;i<a.length;i++)

{

out.println(a[i]+"<br>");

}

p = Pattern.compile("\\+");

a=p.split("dsafasdfdsafsda+dsagfasdfa+sdafds");

out.println(a.length+"<br>");

for(i=0;i<a.length;i++)

{

out.println(a[i]+"<br>");

}

%>

62,614

社区成员

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

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