As much as I like regular expressions, if you're doing this often in your program you'll do better with a small class to represent your tokens. Consider this as a rough sketch:
public class SamToken
{
public string Head { get; set; }
private readonly HashSet<string> properties;
public HashSet<string> Properties{
get{return properties; }
}
public SamToken() : this("") { }
public SamToken(string head)
{
Head = head;
properties = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
}
public void Add(params string[] newProperties)
{
if ((newProperties == null) || (newProperties.Length == 0))
return;
properties.UnionWith(newProperties);
}
public override string ToString()
{
return String.Format("{0}[{1}]", Head,
String.Join("|", Properties));
}
}
Next, you can use a function to parse a token from a string, something among the line of:
public static SamToken Parse(string str)
{
if (String.IsNullOrEmpty(str))
return null;
Match match = Regex.Match(str, @"^(\w*)\[([\w|]*)\]$");
if (!match.Success)
return null;
SamToken token = new SamToken(match.Groups[1].Value);
token.Add(match.Groups[2].Value.Split('|'));
return token;
}
With something like this, it would be easy to add properties:
SamToken token = SamToken.Parse("span[hello|world]");
token.Add("style", "class");
string s = token.ToString();
As you can see, I've only put in a few minutes, but your code can be much more robust and more importantly, reusable. You don't have to rewrite that regex every time you want to check or add a property.