求一正则表达式

jpweng 2007-05-29 04:00:01
小弟不会正则表达式,但现在需用到
特向各位大哥求一好的获取链接地址的正则表达式,如……<a href="http://community.csdn.net/">,要得到http://community.csdn.net/
感激不尽!!
...全文
205 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyisky84 2007-05-29
  • 打赏
  • 举报
回复
此正则表达式可以匹配任何 在wordbegin和wordend中的内容
wuyisky84 2007-05-29
  • 打赏
  • 举报
回复
public string SniffwebCode(string code, string wordsBegin, string wordsEnd)
{
string NewsTitle="";
Regex regex1 = new Regex("" + wordsBegin + @"(?<title>[\s\S]+?)" + wordsEnd + "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
for (Match match1 = regex1.Match(code); match1.Success; match1 = match1.NextMatch())
{
NewsTitle = match1.Groups["title"].ToString();
}
return NewsTitle;
}

在本题中 wordsBegin 为: <a href=" wordsEnd 为: ">
HarleyTung 2007-05-29
  • 打赏
  • 举报
回复
<a\s[^>]*href=(['""]?)(?<url>[^'""\s]*)\1?[^>]*>
gzdiablo 2007-05-29
  • 打赏
  • 举报
回复
<a\s[^>]*href\s*=\s*(['"]?)(?<url>[^'"\s>]*)[^>]*>
补充一下
晓疯馋曰 2007-05-29
  • 打赏
  • 举报
回复
<a.*?href=['""]*(?<url>[^'"" >]*?)['"" >]

我用的这个.
-----------------------
CSDN 论坛助手
http://china-csdn.cn
-过客- 2007-05-29
  • 打赏
  • 举报
回复
如果同时取多个,这样

string yourStr = ...............;
MatchCollection mc = Regex.Matches(yourStr, @"<a\s[^>]*href=(['""]?)(?<url>[^'""\s]*)\1?[^>]*>", RegexOptions.IgnoreCase);
foreach (Match m in mc)
{
richTextBox2.Text += m.Groups["url"].Value + "\n";
}
jarod_d 2007-05-29
  • 打赏
  • 举报
回复
string url = "……<a href="http://community.csdn.net/">";
url = Regex.Replace(url, @".*<a\s+href=\"(?<url>http://[^\"]*)\">.*", "${url}", RegexOptions.IgnoreCase | RegexOptions.Compiled);
-过客- 2007-05-29
  • 打赏
  • 举报
回复
如果是取一个,这样

stirng yourStr = ..............;
string resultStr = "";
Match m = Regex.Match(yourStr, @"<a\s[^>]*href=(['""]?)(?<url>[^'""\s]*)\1?[^>]*>", RegexOptions.IgnoreCase);
if (m.Success)
{
resultStr = m.Groups["url"].Value;
}
qingwuwa 2007-05-29
  • 打赏
  • 举报
回复
操作字符串就可以实现
叶子 2007-05-29
  • 打赏
  • 举报
回复
正则表达式不是用来验证的吗?获取怎么用正则?

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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