Is this a safe way to parse out HTML tag attributes? I needed a super simple parser to get HTML attributes and their values. I didn’t want to load a big library or anything so I made this.

I realize I am making assumptions here, mainly:


Attribute values will be surrounded with either single or double
quotes.
The input string will end with &gt; or /&gt;.


Besides those assumptions are there any other glaring issues here?

Dictionary&lt;string, string&gt; HTMLAttributes(string tag)
{
    Dictionary&lt;string, string&gt; attr = new Dictionary&lt;string, string&gt;();

    MatchCollection matches = 
        Regex.Matches(tag, @"([^\t\n\f \/&gt;""'=]+)(?:=)('.*?'|"".*?"")(?:\s|\/&gt;|\&gt;)");

    foreach (Match match in matches)
    {
        attr.Add(match.Groups[1].Value,
            match.Groups[2].Value.Substring(1, match.Groups[2].Value.Length - 2)
            );
    }

    return attr;
}


Running:

HTMLAttributes("&lt;body class=\" something \" hello='world' /&gt;");


Returns:

{
    class: " something ",
    hello: "world"
}


Sample:

http://ideone.com/ZBaBhB
Whitespace around = is allowed, your regex won't handle that.
Characters in HTML can be encoded, some characters (like &amp;) have to be encoded. For example name="AT&amp;amp;T" should return that the value of name is AT&amp;T.