Regular Expression Visualizer - A Must Have Tool
This week I saw a twitter from Roy Osherove about a new Regular Expression Visualizer he built. If you do not know Roy yet, he is a very smart .NET developer for TypeMock and has done some great work with regular expression tools, such as Regulazy and The Regulator. If you are not familiar with Visualizers then you should get to know how they work.
A Visualizer is simple a tool inside of Visual Studio that allows you to see the data in an object. Custom Visualizers can be created to provide a richer view of an object than the built-in String visualizer where you can just drill into the specific items of an object. The idea being you will be able to have a better experience visualizing the object. In the case of Roy's regular expression visualizer you get a more organized view into how a regular expression is working in the application.
Once you download the zip package containing the components for the Regular Expression Visualizer you can look at the ReadMe.txt file for instructions to install the visualizer. Basically you need to extract a set of dlls to the 'My Documents\Visual Studio 2008\Visualizers' folder. This will install the visualizer in Visual Studio for instance use.
Roy wrote a very simple test to verify the visualizer is installed, but I wanted to take it to a more practical example for you. I changed the example to check a phone number to see if it is valid. The example uses a regular expression to extract the parts of a valid phone number. It then checks to see if there are any valid matches and returns true.

I set a breakpoint in the code before it would return true or false on a match. This allows you to roll the mouse over the matches collection. Here you will see a magnifying glass in the inline watch window. This can then be clicked to display a menu of possible visualizers, in this case the Regular Expression Visualizer. By selecting this visualizer it will display a special dialog.

This dialog provides a breakdown not only of how the regular expression works, but allows you to manipulate how the expression is applied so you can see how it affects the string being parsed. I thing the Pattern Desc tab is extremely useful to see the logic of how a regular expression is executed like no where else I have ever seen. This alone is a great experience for anyone new to regular expressions. It is a great feature to debug a regular expression.
In the top left corner there is a Matches Tab, which shows all the strings contained in the source that match the regular expression. This provides an expandable view into the pieces that match the expression.
When it is all said and done the Regular Expression Visulizer is a great tool for Visual Studio to help build and test regular expressions in .NET applications. I recommend this for any .NET programmer to add to their toolkit to be more productive.
Example Regular Expression Test Project