0

giving error while counting alt tag using regex- Only assignment, call, increment, decrement, and new object expressions can be used as a statement and ; Expected

i want to count img tags which is having alt tag and empty alt tag using c#

 MatchCollection ImgAltTag = Regex.Matches(strIn, "<img[^>]*alt=['"].+['"]", RegexOptions.IgnoreCase | RegexOptions.Multiline);

sample img tags

<img src="alt.png" class="absmiddle" alt="" />
<img src="alt.png" class="absmiddle" />

it should give count 2

  • Use `MatchCollection ImgAltTag = Regex.Matches(strIn, @"]*>", RegexOptions.IgnoreCase);`. You seem to need to match all `img` tags, not just those having `alt` attribute. – Wiktor Stribiżew Nov 10 '15 at 12:27
  • yes but , it should also count img tag which having empty string. like alt="" – krishna mohan Nov 10 '15 at 12:39
  • I'd suggest a HtmlAgilityPack (HTML parser) solution. What do you think? If you are going to use regex for this task, you will soon come back here again (and it is your 3rd question on the same topic already - aren't you tired of that?) – Wiktor Stribiżew Nov 10 '15 at 12:42

2 Answers2

0

Don't use Regex for this. Much easier to use XML Ling

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml =
                "<Root>" +
                "<img src=\"alt.png\" class=\"absmiddle\" alt=\"\" />" +
                "<img src=\"alt.png\" class=\"absmiddle\" />" +
                "</Root>";

            XElement root = XElement.Parse(xml);

            int count = root.Descendants("img").Where(x => x.Attribute("alt") == null || x.Attribute("alt").Value.Length == 0).Count();

        }
    }
}
​
jdweng
  • 33,250
  • 2
  • 15
  • 20
0

If you need to work with HTML, use an HTML parser.

Here is an HtmlAgilityPack based answer.

Suppose you have:

<img src="alt.png" class="absmiddle" alt="" />
<img src="alt.png" class="absmiddle" />
<img src="ff" />

There is 1 img tag you need to obtain as it contains alt. You need an XPath that is //img[@alt] to get all of them, regardless if they have value inside or not. No need to worry about the quotes, either.

public int HtmlAgilityPackGetImgTagsWithAlt(string html)
{
    HtmlAgilityPack.HtmlDocument hap;
    Uri uriResult;
    if (Uri.TryCreate(html, UriKind.Absolute, out uriResult) && uriResult.Scheme == Uri.UriSchemeHttp)
    { // html is a URL 
        var doc = new HtmlAgilityPack.HtmlWeb();
        hap = doc.Load(uriResult.AbsoluteUri);
    }
    else
    { // html is a string
        hap = new HtmlAgilityPack.HtmlDocument();
        hap.LoadHtml(html);
    }
    var nodes = hap.DocumentNode.SelectNodes("//img[@alt]");
    return nodes != null ? nodes.Count : -1;
}

And the result is 1.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563