Click here to Skip to main content
Click here to Skip to main content
Go to top

Regular Expression Tester

, 8 Sep 2005
Rate this:
Please Sign up or sign in to vote.
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:

  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:

  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:

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:

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)

Share

About the Author

Christopher Scholten
Web Developer
Hong Kong Hong Kong
No Biography provided

Comments and Discussions

 
NewsThere's a new version of the RegEx Tester Tool ! PinmemberBucanerO_Slacker1-Mar-08 23:34 
Generalwell done Pinmembermqcan01167-Mar-07 17:41 
GeneralVery Very useful - I added some code to see all matches in string and their groups Pinmembershental4-Dec-06 2:06 
GeneralJust what I wanted Pinmemberwaldermort23-Sep-06 8:24 
GeneralRe: Just what I wanted PinmemberChristopher Scholten23-Sep-06 19:32 
GeneralRe: Just what I wanted Pinmemberantoine beloeuvre17-Oct-06 4:34 
GeneralRe: Just what I wanted Pinmemberjesuscheung16-Mar-07 15:37 
Nice and simple.

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 8 Sep 2005
Article Copyright 2005 by Christopher Scholten
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid