There are already many regular expression testers out there with many advanced features. This one is a simple tester which allows one
string to be tested against a standard regular expression, with feedback on the result in both text and graphical format, as either the regular expression, or the test
string is typed.
Format / Environment
The code is written in C# in Visual Studio 2003 format (.NET Framework 1.1).
There are two textboxes allowing input on the form - the top one for the regular expression to be input, the lower allowing a
string to be tested against the regular expression. As text in either textbox is updated, the text is checked against the regular expression for a match. One of three mutually exclusive outcomes will result when modification of some text occurs:
string matches the regular expression pattern - in this case the Result Light turns green:
string does not match the regular expression pattern (the first digit of the year is a character) - in this case the Result Light turns red:
- The regular expression
string is invalid (missing a right-parenthesis ')' in the expression) - in this case the Result Light turns orange, and the error details are listed in the output textbox:
When a regular expression is composed, matching
string expressions may be divided into groups based on which sub-sections of a pattern are wrapped in parentheses (braces, brackets '(', ')', or whatever you want to call them). These groups can be nested. This program simply gets the top level groups, and lists them to the right of the input box when a successful match is made. Refer to the first example image (Usage outcome 1 - match). The simple crude regular expression to test for a date has an overall group match (Group 0, for the entire expression), and a match on each set of digits in the date. This is because each of the numbers in the date expression is wrapped in parentheses. For example, based on this, using the crude expression (which no-one would, because there are so many better date-checking expressions available!), the text from group 2 could be captured as the month.
The code is absurdly simple. There is one method (
CheckMatch()) which checks the text to be checked against the regular expression, and updates the groups and status information. This method is called when:
- the form is loaded,
- the regular expression is changed, or
- when the text to be checked changes.
This allows for real-time feedback on the result of the last action. Here is the (edited) relevant section of code for copy-pasters:
private void CheckMatch()
rxImageCode = new Regex(txtRegularExpression.Text);
catch (Exception ex)
... notify of invalid regular expression ...
Match rxMatch = rxImageCode.Match(txtTextToTest.Text);
... indicate success ...
for(int i = 0; i < rxMatch.Groups.Count; i++)
... indicate failure ...
Regular expressions are checked as they are typed, hence it's almost certain that as one types, at some point the regular expression will be invalid. A
catch block is used around the check for a valid regular expression, thus the first time an invalid regular expression is entered, an error will occur. The first time an error occurs, there will be a short delay in responsiveness, as the error is handled on the same thread as the owner of the form. This delay does not occur for subsequent errors that are caught. Finally, some of my variables are named after "Image Codes" because I'm currently working on a program to batch-update my photos and videos, and I assign each of my files a code in a specific format.
There's also a handy link to http://www.regexlib.com/CheatSheet.htm, which provides a quick reference to regular expression codes. I can never remember all of the codes - can you?
Using this tool, one can quickly compose a regular expression with instant feedback as to whether a given
string matches the expression (the tool was written more for composing expressions, rather than testing existing expressions).
- 8th September, 2005: Initial post