111,093
社区成员




Regex re = new Regex("<img\\s+[^>]*alt=[\'\"](?<alt>[^\'\"]+)?[\'\"][^>]*src=[\'\"](?<url>http://[^\'\"]+)[\'\"][^>]*>", RegexOptions.IgnoreCase);
Regex re = new Regex("(?i)<img(?=[^>]*?alt=([\"']?)(?<alt>(?:(?!\\1).)*)\\1)[^>]*?src=([\"']?)(?<src>(?:(?!\\2).)*)\\2[^>]+>");
string imgurl = "<img src='http://www.a6.com/aaa.jpg' title='heeeo' alt='hello!' />";
MatchCollection mc = re.Matches(imgurl);
foreach (Match m in mc)
{
string url = m.Groups["src"].Value;
string alt = m.Groups["alt"].Value;
richTextBox2.Text = string.Format("Url={0}; Alt={1}", url, alt);
}
/*-----输出-----
Url=http://www.a6.com/aaa.jpg; Alt=hello!
*/
static void Main(string[] args)
{
Regex re = new Regex("<img[^>]+?(alt|src)=([\"'])(?<src>[^\\2]*?)\\2\\s+?(src|alt)=([\"'])(?<alt>[^\\4]*?)\\4[^>]+?>", RegexOptions.IgnoreCase);
//Regex re = new Regex("<img\\s+([^>]*)[/]?>", RegexOptions.IgnoreCase);
string imgurl = "<img alt='hello!' src='http://www.a6.com/aaa.jpg' title='heeeo' />";
MatchCollection mc = re.Matches(imgurl);
foreach (Match m in mc)
{
string url = m.Groups["src"].Value;
string alt = m.Groups["alt"].Value;
Console.WriteLine("Url={0}; Alt={1}", url,alt);
}
Console.ReadLine();
}