++++++++ 求正则表达式:怎么取得之间的字符串 ++++++++

laibagefei 2005-12-13 06:06:35
求正则表达式:怎么取得<a href="xxxxxx">和</a>之间的字符串?

比如:<a href="xxxxxx">北京海淀区颐和园</a>,就取‘北京海淀区颐和园’,其它不要。

xxxxxx为网址,可能带参数

请各位老大给一个可用的算法,谢谢
...全文
230 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xu51mm 2005-12-21
  • 打赏
  • 举报
回复
<[aA].*?>|<[/][aA]>
  • 打赏
  • 举报
回复
String testString = "<a href=\"http://www.com.cn/a/b.jsp?name=value\">北京海淀区颐和园</a>";
String regExp2 ="(<a\\s+href\\s*=(\\s*(\"[^\"]*\"|[^\\s>])[^>]*)>([^<]*))(.*)(</a>)";
r = testString.replaceAll(regExp2,"$4");
System.out.println(r);
laibagefei 2005-12-14
  • 打赏
  • 举报
回复
谢谢 我试一下
niko7 2005-12-13
  • 打赏
  • 举报
回复
通过抗干扰测试,好像还是这样好点:

public static void main(String[] args)
{
String testString = "<a href=\"http://www.com.cn/a/b.jsp?name=value\">><北京海淀区颐和园</a>";
Pattern p = Pattern.compile("<[^>]*>(.*)</[^>]*>");
Matcher m = p.matcher(testString);
while (m.find())
{
System.out.println(m.group(1));
}
}
liu_you 2005-12-13
  • 打赏
  • 举报
回复
public static void main(String[] args)
{
String reg="<[^>]*>([^<]*)<[^>]*>";
Pattern pattern=Pattern.compile(reg);
String s="<a href=\"xxxxxx\">北京海淀区颐和园</a>";
Matcher matcher=pattern.matcher(s);
while(matcher.find())
{
System.out.print(matcher.group(1));
}
}
lanseqingxu 2005-12-13
  • 打赏
  • 举报
回复
这个链接最规则了,应该很好取的,匹配表达式如下
<a\\s+href\\s*=(\\s*(\"[^\"]*\"|[^\\s>])[^>]*)>([^<]*)</a>
想取什么自己去相应的组中取吧
niko7 2005-12-13
  • 打赏
  • 举报
回复
public static void main(String[] args)
{
Untitled2 u = new Untitled2();
String testString = "<a href=\"http://www.com.cn/a/b.jsp?name=value\">北京海淀区颐和园</a>";
String r = testString.replaceAll("^<a.*\\\"\\s*>|</a>$","");
System.out.println(r);
}
laibagefei 2005-12-13
  • 打赏
  • 举报
回复
一定要用正则了,你有什么高招吗?
niko7 2005-12-13
  • 打赏
  • 举报
回复
一定要用正则表达式提取吗?取 > 和 < 之间的内容啊。


如果用正则表达式擦掉 < 和 > 这件的内容,那么剩下的也行啊。
另外,构造一个xml,然后提取节点 a 的内容,也是可以的嘛。

81,092

社区成员

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

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