There is no problem to get src or alt separately, but how can you get both at the same time as the group name.
We should keep in mind that alt can be left or right of src.
I'm in a hurry, so I found a quick solution by creating 3 group names for src, and for alt. I know that we can do it much better.
private void GetFirstImage(string newHtml, out string imgstring, out string imgalt)
{
imgalt = "";
imgstring = "";
string pattern = "(?<=<img(?<name1>\\s+[^>]*?)src=(?<q>['\"]))(?<url>.+?)(?=\\k<q>)(?<name2>.+?)\\s*\\>";
try
{
if (Regex.IsMatch(newHtml, pattern))
{
Regex r = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
imgstring = r.Match(newHtml).Result("${url}");
string tempalt = "", tempalt2;
tempalt = r.Match(newHtml).Result("${name1}");
tempalt2 = r.Match(newHtml).Result("${name2}");
try
{
pattern = "alt=(?<q>['\"])(?<alt>.+?)(?=\\k<q>)";
if(!String.IsNullOrEmpty(tempalt.Trim()))
{
r = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
if (Regex.IsMatch(tempalt, pattern))
{
imgalt = r.Match(tempalt).Result("${alt}");
}
}
if(String.IsNullOrEmpty(imgalt) && !String.IsNullOrEmpty(tempalt2.Trim()))
{
r = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
if (Regex.IsMatch(tempalt2, pattern))
{
imgalt = r.Match(tempalt2).Result("${alt}");
}
}
}
catch{ }
}
}
catch{}
}
source
share