110,532
社区成员
发帖
与我相关
我的任务
分享
(?<=<IMG[^src]+?src=")[^"]+?(?="[^>]+?>)
string str="..";
string RegexString = @"<IMG.+?src=""(?<SRC>[^<].*?)"".+?>";
string[] SRC = GetRegValue(str, RegexString, "SRC", true);
/// <summary>
/// 正则表达式取值
/// </summary>
/// <param name="HtmlCode">源码</param>
/// <param name="RegexString">正则表达式</param>
/// <param name="GroupKey">正则表达式分组关键字</param>
/// <param name="RightToLeft">是否从右到左</param>
/// <returns></returns>
public string[] GetRegValue(string HtmlCode , string RegexString,string GroupKey,bool RightToLeft)
{
MatchCollection m;
Regex r;
if (RightToLeft == true)
{
r = new Regex(RegexString, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.RightToLeft);
}
else
{
r = new Regex(RegexString, RegexOptions.IgnoreCase | RegexOptions.Singleline);
}
m= r.Matches(HtmlCode);
string[] MatchValue = new string[m.Count];
for (int i = 0; i < m.Count; i++)
{
MatchValue[i] = m[i].Groups[GroupKey].Value;
}
return MatchValue;
}
MatchCollection mc = Regex.Matches(str, @"<img.*?src=""(?<url>[^""]*)""[^>]*>", RegexOptions.IgnoreCase);
foreach (Match m in mc)
{
Console.WriteLine(m.Groups["url"].Value);
}