0

I want to get all the names of class used in my html file. I've tried so far in C#

String data = Uri.UnescapeDataString(TextBox1.Text);        
    List<string> allClass = new List<string>();
    Match match = Regex.Match(data, "class=\"[^#]+\"");
    if (match.Success)
    {
        Console.WriteLine(match.Captures[0].Value); // Will output "#item3#"
    }

but this is not giving the desired result. as my code is

<div class="dialogBodyWrapper">
                <div class="dialogBoxContentParent">
                    <p class="mediumText">Changing your authentication details will log you out from the current session
                        and requires re-login with new credentials. Would You like to proceed?</p>
                </div>
                <div class="clear"></div>
            </div>

I want class names in list like dialogBodyWrapper,dialogBoxContentParent,mediumText and clear.

I've tried many regex expression but none is working for me. Please help me.

Sumit Chourasia
  • 2,394
  • 7
  • 30
  • 57
  • 1
    no http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – ASA Apr 09 '14 at 15:09
  • @Traubenfuchs > if you read the thread, you will see this is still in discussion. In that particular case, I don't see why the OP couldn't use regex ... but I may be wrong. – Laurent S. Apr 09 '14 at 15:14

3 Answers3

2

Save yourself a huge amount of pain, and just use the Html Agility pack from the start...

http://htmlagilitypack.codeplex.com/

James Gaunt
  • 14,631
  • 2
  • 39
  • 57
1

I strongly agree you should use the HtmlAgilityPack from anything strenuous - however - if this is a one-off script, you may be able to use something like:

var classes = Regex
    .Matches(html, @"class=""(.*?)""")
    .Cast<Match>()
    .Select(m => m.Groups[1].Value);
Dave Bish
  • 19,263
  • 7
  • 46
  • 63
1

part of your problem was that you used match instead of matches. also i'd use a regex such as class="[^"]+" easy to comprehend regex when you get back to it later.

Dbl
  • 5,634
  • 3
  • 41
  • 66