Click here to Skip to main content
15,879,095 members
Articles / Desktop Programming / Windows Forms
Article

Regular Expression Tester

Rate me:
Please Sign up or sign in to vote.
3.50/5 (8 votes)
8 Sep 2005CPOL3 min read 67K   1.1K   19   7
A simple Regular Expression 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 are typed.

Introduction

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).

Usage

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:

  1. The string matches the regular expression pattern - in this case the Result Light turns green:

    Image 1

  2. The 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:

    Image 2

  3. 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:

    Image 3

Groups

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.

Sample Code

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:

  1. the form is loaded,
  2. the regular expression is changed, or
  3. 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:

C#
using System.Text.RegularExpressions;

...

private void CheckMatch() 
{
    Regex rxImageCode;

    try 
    {
        // Load the regular expression
        rxImageCode = new Regex(txtRegularExpression.Text);
    } 
    catch (Exception ex) 
    {
        // Invalid regular expression - indicate reason in 
        //    output textbox
        ... notify of invalid regular expression ...
        return;
    }
    
    // Test regular expression
    Match rxMatch = rxImageCode.Match(txtTextToTest.Text);

    // Was there a match?
    if(rxMatch.Success) 
    {
        ... indicate success ...

        /// Iterate over each of the (top level) groups
        /// in the regular expression, as per the  
        /// parentheses: '(' and ')' characters, and list 
        /// the matching string to the right of the input
        for(int i = 0; i < rxMatch.Groups.Count; i++) 
        {
            // Do something with value of the group:
            ... rxMatch.Groups[i].Value
        }
    } 
    else 
    {
        ... indicate failure ...
    }
}

Shortcomings

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 try-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.

Feature!

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?

Conclusion

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).

History

  • 8th September, 2005: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
NewsThere's a new version of the RegEx Tester Tool ! Pin
Pablo Osés1-Mar-08 23:34
Pablo Osés1-Mar-08 23:34 
Generalwell done Pin
JohnnyQiang7-Mar-07 17:41
JohnnyQiang7-Mar-07 17:41 
GeneralVery Very useful - I added some code to see all matches in string and their groups Pin
shental4-Dec-06 2:06
shental4-Dec-06 2:06 
GeneralJust what I wanted Pin
Waldermort23-Sep-06 8:24
Waldermort23-Sep-06 8:24 
GeneralRe: Just what I wanted Pin
Christopher Scholten23-Sep-06 19:32
professionalChristopher Scholten23-Sep-06 19:32 
Thanks. It's nice to get some positive feedback.

Being simple and providing immediate feedback was the purpose of the tool.
GeneralRe: Just what I wanted Pin
antoine beloeuvre17-Oct-06 4:34
antoine beloeuvre17-Oct-06 4:34 
GeneralRe: Just what I wanted Pin
jesuscheung16-Mar-07 15:37
jesuscheung16-Mar-07 15:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.