.NET Framework Developer Center > .NET Development Forums > Regular Expressions > What the RegEx to identify STYLE tags?
Ask a questionAsk a question
 

AnswerWhat the RegEx to identify STYLE tags?

  • Tuesday, November 03, 2009 9:42 PMWiznet972 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello..

    I need the template to identify STYLE tags in html file.

    like:
       <div id="F" style="height:2px; color:red" runat="Server" >
           <asp:label />
       </div>

    So I need it to find: height:2px; color:red

    please help me! it's very urgent to me!


Answers

  • Wednesday, November 04, 2009 3:54 PMAhmad Mageed Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    How about this:

    string input = @"<p>style=""height:1px;""</p>
    <div id=""F"" style=""height:2px; color:red"" runat=""Server"">
        <p id=""p"" style=""height:3px; color:green"" />
    </div>";
    
    string pattern = @"<[^>]+style=""(?<style>[^""]+)""[^>]*>";
    
    foreach (Match m in Regex.Matches(input, pattern))
    {
        if (m.Success)
        {
            m.Value.Dump();
            string result = m.Groups["style"].Value;
            Console.WriteLine(result);
        }
    }
    

    If you need to match "style" with different cases, use Regex.Matches(input, pattern, RegexOptions.IgnoreCase) instead.
    Document my code? Why do you think it's called "code"?

All Replies

  • Wednesday, November 04, 2009 3:54 PMAhmad Mageed Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    How about this:

    string input = @"<p>style=""height:1px;""</p>
    <div id=""F"" style=""height:2px; color:red"" runat=""Server"">
        <p id=""p"" style=""height:3px; color:green"" />
    </div>";
    
    string pattern = @"<[^>]+style=""(?<style>[^""]+)""[^>]*>";
    
    foreach (Match m in Regex.Matches(input, pattern))
    {
        if (m.Success)
        {
            m.Value.Dump();
            string result = m.Groups["style"].Value;
            Console.WriteLine(result);
        }
    }
    

    If you need to match "style" with different cases, use Regex.Matches(input, pattern, RegexOptions.IgnoreCase) instead.
    Document my code? Why do you think it's called "code"?