How to count img tags that does not have alt tags using regex

advertisements

How to count img tags which has no alt tags in by using regex?

 MatchCollection AltTag = Regex.Matches(
     strIn,
     "<img .*? />",
     RegexOptions.IgnoreCase | RegexOptions.Multiline
 );
 lblImgTags.Text = "We found " + AltTag.Count.ToString() + " images on this website ";

I am counting img tags like this but how can I count img tags which have no alt tags?


How to count img tags which has no alt tags by using regex

You can't. You cannot use regular expressions to parse HTML. You may think you have a sufficiently-contained use case that you can. So have I. I was wrong, and with respect, I'm afraid you are as well.

Use an HTML parser to parse HTML. There are several, such as NSoup, which is a .Net port of the excellent Java JSoup library. But that is just one option of many.