rss
twitter
    Find out what I'm doing, Follow Me :)

Regular Expresion for Html Tag Matching

Technorati Tags: ,,

Regular Expresion for Html Tag Matching

Consider you have string/text or file which you need to parse to find the Html Tags inserted in the content or replacing the Html content.

Here is the pattern which matches Html Tags

Pattern: @"</?[a-z][a-z0-9]*[^<>]*>"

Here is the sample code to test

   Regex reg = new Regex(pattern);

string inputString = "<font size='2' color='#0000ff'><font size='2' color='#0000ff'> " +
     "<table id='mytable'><tr><td>TUE</td></tr><tr><td>21 JUL</td></tr><div><tr><td>"  +
         "<img src='images/sunnf.gif'></td></tr><tr><td>Clear</td></tr></div><tr><td>" +
         "</td></tr><tr><td>High</td></table>";

         MatchCollection matches = reg.Matches(inputString);

         foreach (Match match in matches)
         {
             Console.WriteLine(match.Value);
         }
         Console.ReadLine();

here pattern considers only the lower case html Tags.
as Html tags are in lower case it should not be a concern.

0 comments: