Regex正则表达式 提取 HTML中 SRC的值

xzgtysx 2009-09-02 09:40:16
大致意思是这样的:
1.我从数据库中提取新闻的内容字段出来,此处命名为nr。
2.由于采用了fckeditor控件,所以nr现在已经是html格式的内容了。
3.现在要将nr中出现的第一个img中的src读取出来,然后赋值给B。
nr的格式如下:
<img height="469" width="452" alt="" src="/UploadFile/FCKeditor/image/123.jpg" />
B的值应该是上面带下划线的值。。。。。。


请大家帮忙。。。。。
...全文
994 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
Tiantiandiandian 2009-09-02
  • 打赏
  • 举报
回复
@"src=\042.{0,500}\042"
wuyq11 2009-09-02
  • 打赏
  • 举报
回复
string pattern = "<IMG\\s+src=\"(?<src>[^\"]+?)\"[\\s\\S]*?>";
Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection m = r.Matches("");
foreach(Match m in mc)
{
Console.WriteLine(mc.Groups["src"].Value);
}

十八道胡同 2009-09-02
  • 打赏
  • 举报
回复
static void Main(string[] args)
{
string str=@"<img height=""469"" width=""452"" alt="""" src=""/UploadFile/FCKeditor/image/123.jpg"" /> ";
Regex re = new Regex(@"<img[^(src)]*src=""(?<src>[^""]*)""");
Console.WriteLine(re.Match(str).Groups["src"].Value);

}
nyq1999 2009-09-02
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 yutian_01261027 的回复:]
UP
引用 6 楼 pt1314917 的回复:
C# code <img\s[^(src)]*\s*src="([^"]*)" />//这样稍微好点,还可以匹配只有src属性的情况。

[/Quote]
不好吧,如果漏写了'"' scr=1.jpg 就比较危险了
十八道胡同 2009-09-02
  • 打赏
  • 举报
回复
[^(src)]* 的意思是除了左右括号、s、r、c五个字符外的任意个任意字符 这里之所以能匹配是碰巧了src前面没有这五个字符而已

---------------
额,是哦,
(?:(?!src)[^<>])*
yutian_01261027 2009-09-02
  • 打赏
  • 举报
回复
UP
[Quote=引用 6 楼 pt1314917 的回复:]
C# code<img\s[^(src)]*\s*src="([^"]*)" />//这样稍微好点,还可以匹配只有src属性的情况。
[/Quote]
pt1314917 2009-09-02
  • 打赏
  • 举报
回复

<img\s[^(src)]*\s*src="([^"]*)" />
//这样稍微好点,还可以匹配只有src属性的情况。
pt1314917 2009-09-02
  • 打赏
  • 举报
回复
<img\s[^(src)]*\ssrc="([^"]*)" /> 
flyerwing 2009-09-02
  • 打赏
  • 举报
回复
学习,顶。
我姓区不姓区 2009-09-02
  • 打赏
  • 举报
回复

B = Regex.Match(nr, "<img[^>]*src=[\"']*([^>\"']+)[\"']*\\s*/>").Groups[1].Value;
shenymce 2009-09-02
  • 打赏
  • 举报
回复
up
编程有钱人了 2009-09-02
  • 打赏
  • 举报
回复

/// <summary>
/// 获得图片的路径并存放
/// </summary>
/// <param name="M_Content">要检索的内容</param>
/// <returns>IList</returns>
public static IList<string> GetPicPath(string M_Content)
{
IList<string> im = new List<string>();//定义一个泛型字符类
Regex reg = new Regex(@"<img.*?src=""(?<src>[^""]*)""[^>]*>", RegexOptions.IgnoreCase);
MatchCollection mc = reg.Matches(M_Content); //设定要查找的字符串
foreach(Match m in mc)
{
im.Add(m.Groups["src"].Value);
}
return im;

}
24K純帥 2009-09-02
  • 打赏
  • 举报
回复
up
mawering 2009-09-02
  • 打赏
  • 举报
回复
帮顶了,学习一下!
wackyboy 2009-09-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 pt1314917 的回复:]
C# code<img\s[^(src)]*\ssrc="([^"]*)" />
[/Quote]
害人不浅阿
[^(src)]* 的意思是除了左右括号、s、r、c五个字符外的任意个任意字符 这里之所以能匹配是碰巧了src前面没有这五个字符而已
这样吧
@"<img\s(?:(?!src)[^<>])*src=""([^""]*)""/>"

取Groups[1].Value
wackyboy 2009-09-02
  • 打赏
  • 举报
回复

string input = @"<img height=""469"" width=""452"" alt="""" src=""/UploadFile/FCKeditor/image/123.jpg"" /> ";
Match m = Regex.Match(input, @"(?i)(?<=<img\b[^<>]*src\s*=\s*[""])[^""]*");
string b = m.Value;
wb1125 2009-09-02
  • 打赏
  • 举报
回复
C#测试过。没问题的:
string regexstr = "<img[\\s\\w\"=]+?(?<=src=\")(?<imgsrc>[/\\w\\.]+)(?=\")";
Regex.Match(s, regexstr).Groups["imgsrc"].Value的值即为需要的字符串。

110,571

社区成员

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

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

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