June 9, 2011

Regex Matches Tester

Can find and list matching regular expressions in a string
internal class TestRegexMatches
{

    public void Test()
    {
        string typeName = typeof(double).ToString().Replace('.', '_');
        string name = "Fudge Factor".Replace(" ", "_");
        string plugin = "Smb.Orca.Theo.Samba.Delta, Version=4.20111.99.0, Culture=neutral, PublicKeyToken=19ae4a476ca5c63x";
        string[] splitStr = plugin.Split(',');
        plugin = splitStr[0].Replace('.', '_');

        string id = typeName + "\t" + name + "\t" + plugin;
        int ix=1;
        MatchCollection matches = Regex.Matches(id, @"[^\t]+");
        foreach (Match match in matches)
        {
            Console.WriteLine("Match " + ix++.ToString() + ": " + match.Value);
        }
    }
}
produces the output
Match 1: System_Double
Match 2: Fudge_Factor
Match 3: Smb_Orca_Theo_Samba_Delta

No comments: