大致意思是这样的:
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的值应该是上面带下划线的值
请大家帮忙。。
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的值应该是上面带下划线的值
请大家帮忙。。
/// <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;
}
B = Regex.Match(nr, "<img[^>]*src=[\"']*([^>\"']+)[\"']*\\s*/>").Groups[1].Value;
<img\s[^(src)]*\s*src="([^"]*)" />
//这样稍微好点,还可以匹配只有src属性的情况。
{
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); }
Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection m = r.Matches("");
foreach(Match m in mc)
{
Console.WriteLine(mc.Groups["src"].Value);
}
string regexstr = "<img[\\s\\w\"=]+?(?<=src=\")(?<imgsrc>[/\\w\\.]+)(?=\")";
Regex.Match(s, regexstr).Groups["imgsrc"].Value的值即为需要的字符串。
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;
[^(src)]* 的意思是除了左右括号、s、r、c五个字符外的任意个任意字符 这里之所以能匹配是碰巧了src前面没有这五个字符而已
这样吧@"<img\s(?:(?!src)[^<>])*src=""([^""]*)""/>"
取Groups[1].Value
额,是哦,
(?:(?!src)[^<>])*