Regular Expression Workbench

Eric Gunnerson (EricGu@Microsoft.Com)

8/8/2002

Introduction

Regular expressions are a really powerful facility, but they're a bit tough to learn. I've written this workbench to make it easier to create and understand regular expressions.

If you find any bugs - especially those where the tooltip or interpret feature doesn't work - please send them to me.

Using a regular expression

To learn how to use the workbench, it's useful to use a sample regular expression. The following example can be used to match telephone numbers. For a number such as:

555-1212

The following regular expression can be used:

(?<Number>\d\d\d-\d\d\d\d)

Type the regex into the Regex editbox, and then type the phone number into the Strings editbox. Press Execute to try to match the regular expression against the string. The results of the match will apear in the output window. Hover over the regular expression to see what the various parts of the regular expression mean.

Interface Details

1) Regular Expression editbox

Use this editbox to enter the regular expression. You can either type the expression directly, or use the Add button to enter a specific regular expression element. Once the expression is entered, you can hover over it to understand what the different parts of the regular expression mean.

When hover is enabled, the selection in the editbox will be changed as you hover over different areas. It's sometimes annoying to have this happen when you're editing a regex, so you can turn this off by unchecking the Hover Interpret checkbox

You can write the regex using multiple lines and comments, if you turn on the IgnorePatternWhitespace option. For example, you could write the telephone number regex as:

(?<Number>         # Capture to Number
\d\d\d                 # Match 3 digits
-                      # Match -
\d\d\d\d               # Match 4 digits
)                  # End of capture

which makes it much easier to understand. Note that if you use this option, you'll have to escape any whitespace (ie '\ ' to match a space).

2) Test string editbox

This editbox is used to specify the strings that are tested against the regex. If there is more than one line, each line will be passed to the regex for a match. If the One String checkbox is checked, a single string containing all the lines and the line separators will be created and tested against the regex

3) Output window

This window display the match results of the regular expression

4) Add Button

Use this button to add a regular expression construct at the current location in the regex.

5) Option checkboxes

These checkboxes are used to set the RegexOptions options that are used to create the regular expression object. Hover over the checkboxes to see what they do.

6) Interpret Button

Interpret the regular expression and put the results into the output window. This is the same output as hovering over a regex, but it interprets the whole regex at once.

7) Execute Button

Try to match the test strings with the regex

8) Split Button

Call regex.split with this regex

9) Create code buttons

Create code to create this regex in either C# or VB format. The text will be displayed in the output window and placed on the clipboard

10) Make assembly button

Create a .NET assembly with this assembly in it.

11) Timing Information

Profile the execute of the regular expression. Set the number of interations to perform, execute the regex, and the compile and execution times will be displayed in the editbox.

12) Hover Interpret checkbox

Enable or disable the hover function on the regex editbox

13) Paste C# Button

Take the regex creation code that was created by the C# create code button, and parse it back to the regex and options

14) About Button

15) One String checkbox

Treat the lines in the test editbox as one multiline text rather than separate lines