2

My HTML string is like this , stored in a variable named sourceCode

    <ul class="yom-list col first" style="width:33.333333333333%">
    <li class="first">
      <a href="/india/andaman-and-nicobar-islands/">
        <span>Andaman and Nicobar Islands</span>
      </a>
    </li>
      <li>
      <a href="/india/jammu-and-kashmir/">
        <span>Jammu and Kashmir</span>
      </a>
    </li>
    <li class="last">
      <a href="/india/andhra-pradesh/">
        <span>Andhra Pradesh</span>
      </a>
    </li>
      <li>
      <a href="/india/jammu-and-kashmir/">
        <span>Jammu and Kashmir</span>
      </a>
    </li>

  </ul>

I want to convert it in to a generic List So that I can access the data inside it in my code like href, name etc.. I have tried something like this

            foreach (Match match in Regex.Matches(sourceCode, @"<li><a href=""(?<url>[^""])</a></li>"))
            items.Add(new Item()
            {

                name = match.Groups["span"].Value, // i don't know how to get value inside that span
                url = match.Groups["url"].Value,

            });

But it does not work, Probably the regex is wrong. Can any one tell me what I am doing wrong? Note: I can't use HTMLAgilityPack in this project

2
  • If you are using XHTML, how about trying to use an XML Parser? Commented Jul 17, 2014 at 7:20
  • @rhughes This is just a string , which holds html tags Commented Jul 17, 2014 at 7:21

1 Answer 1

2

Try the below regex to get the values between <a href> tag and <span> tag only if it is present inside <li> tag.

/<li>\s*<a href=\"(?<url>[^"]*)\">\s*<span>(?<span>[^<]*)<\/span>/m

DEMO

Your c# code would be,

Regex rgx = new Regex(@"<li>\s*<a href=""(?<url>[^""]*)"">\s*<span>(?<span>[^<]*)</span>");
foreach (Match m in rgx.Matches(input))
{
Console.WriteLine(m.Groups["url"].Value);
Console.WriteLine(m.Groups["span"].Value);
}

IDEONE

Sign up to request clarification or add additional context in comments.

2 Comments

I am not able to add this regex inside my C# code Probably the syntax is different
@Athul see the IDEONE link on my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.