Saturday, June 2, 2012

Regular Expression to get the attribute value from html string


public static string ExtractAttributeValueFromHtmlElement(string elementOuterHtml, string attributeName)
        {
            Match m;
            string HRefPattern = "title\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))";
 
            m = Regex.Match(elementOuterHtml, HRefPattern,
                            RegexOptions.IgnoreCase | RegexOptions.Compiled);
            if (!m.Success)
            {
                return null;
            }
            return m.Groups[1].ToString();                            
        }


pattern = style=\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))
input   = <td style="width:100px;height:100px;">Pramod</td>
result  = 
             style="width:100px;height:100px;"
             width:100px;height:100px;

No comments:

Post a Comment