Asp.net 正则表达式 取图片路径

live517 2009-04-24 09:53:55
我写有这样的正则表达式 目的是获得FCkeditor中的图片路径 ,可发现 来Src ="*.*"后面得属性也取出来了,align="absMiddle 不知道有没有更准确的方法~~。
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"<img[^>].*?src=""(?<src>[^""].*)""[^>].*?>",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.MatchCollection m = reg.Matches(str); //设定要查找的字符串
if (m.Count > 0)
{
return (m[0].Groups["src"].ToString());
}
else
{

reg = new System.Text.RegularExpressions.Regex(@"<input[^>].*?src=""(?<src>[^""].*)""[^>].*?>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
m = reg.Matches(str);
if (m.Count > 0)
{
return (m[0].Groups["src"].ToString());
}
else
{
return "";
}



}
...全文
145 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
编程有钱人了 2009-04-25
  • 打赏
  • 举报
回复

protected void Button1_Click(object sender, EventArgs e)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.163.com");
request.Timeout = 20000;
request.ServicePoint.ConnectionLimit = 100;
request.ReadWriteTimeout = 30000;
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode != HttpStatusCode.OK)
return;
StreamReader sr = new StreamReader(response.GetResponseStream());

IList<string> ls = GetPicPath(sr.ReadToEnd());
for (int i = 0; i < ls.Count;i++ )
{
Response.Write(ls[i].ToString()+"<br/>");

}

Response.End();
}
public IList<string> GetPicPath(string M_Content)
{
IList<string> im = new List<string>();//定义一个泛型字符类
Regex reg = new Regex(@"<img.*?src=""(?<href>[^""]*)""[^>]*>", RegexOptions.IgnoreCase);
MatchCollection mc = reg.Matches(M_Content); //设定要查找的字符串
foreach (Match m in mc)
{
im.Add(m.Groups["href"].Value);
}
return im;

}

你把"<img.*?src=""(?<href>[^""]*)""[^>]*>
换成"<input.*?src=""(?<href>[^""]*)""[^>]*>", 就OK
zsuswy 2009-04-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 live517 的回复:]
还是不对 我之前那个 能截取出图片路径 不过连路径后面的也截出来了如:/Upload/images/Article/images_2(5).jpg" vspace="2 我只想要路径
[/Quote]
Regex r = new Regex(@"<img src=""(?<src>[^""]*)""+?[^>]*>");
System.Text.RegularExpressions.MatchCollection m = r.Matches("<img src=\"http://cc.com/aabc.jpg\" vspace=\"2\" />");
Console.WriteLine(m[0].Groups["src"].ToString());
------------------------------------
输出:
http://cc.com/aabc.jpg
live517 2009-04-25
  • 打赏
  • 举报
回复
还是不对 我之前那个 能截取出图片路径 不过连路径后面的也截出来了如:/Upload/images/Article/images_2(5).jpg" vspace="2 我只想要路径
-过客- 2009-04-24
  • 打赏
  • 举报
回复
不清楚你的需求,看你的代码猜的

MatchCollection mc = Regex.Matches(yourStr, @"(?<=<(?:input|img)(?:(?!src)[\s\S])*src=(['""]?))[^""'\s>]*(?=\1)", RegexOptions.IgnoreCase);
foreach (Match m in mc)
{
richTextBox2.Text += m.Value + "\n";
}
live517 2009-04-24
  • 打赏
  • 举报
回复
不对 都没截取出来~~
谢谢le ~
wuyq11 2009-04-24
  • 打赏
  • 举报
回复
Regex regex = new Regex(@"<img.*src=[\']?([^\']*)[\']?[^/]*[/]?>”, RegexOptions.Compiled | RegexOptions.IgnoreCase );

foreach(Match matchItem in regex.Matche(""))
{
if(matchItem.Groups[1]!= null )
{
str+=matchItem.Groups[1].Value;
}
}
参考

62,268

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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