Click here to Skip to main content
14,743,934 members
Articles » General Programming » String handling » Regular Expressions
Posted 8 Sep 2005


19 bookmarked

Regular Expression Tester

Rate me:
Please Sign up or sign in to vote.
3.50/5 (8 votes)
8 Sep 2005CPOL
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.


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:

  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


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;

        // 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 ...
    // Test regular expression
    Match rxMatch = rxImageCode.Match(txtTextToTest.Text);

    // Was there a match?
        ... 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
        ... 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 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.


There's also a handy link to, 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


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


About the Author

Christopher Scholten
Systems Engineer
United States United States
No Biography provided

Comments and Discussions

NewsThere's a new version of the RegEx Tester Tool ! Pin
Pablo Osés2-Mar-08 0:34
MemberPablo Osés2-Mar-08 0:34 
Generalwell done Pin
JohnnyQiang7-Mar-07 18:41
MemberJohnnyQiang7-Mar-07 18:41 
GeneralVery Very useful - I added some code to see all matches in string and their groups Pin
shental4-Dec-06 3:06
Membershental4-Dec-06 3:06 
GeneralJust what I wanted Pin
Waldermort23-Sep-06 9:24
MemberWaldermort23-Sep-06 9:24 
GeneralRe: Just what I wanted Pin
Christopher Scholten23-Sep-06 20:32
professionalChristopher Scholten23-Sep-06 20:32 
GeneralRe: Just what I wanted Pin
antoine beloeuvre17-Oct-06 5:34
Memberantoine beloeuvre17-Oct-06 5:34 
GeneralRe: Just what I wanted Pin
jesuscheung16-Mar-07 16:37
Memberjesuscheung16-Mar-07 16: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.