MSR Rex

 

Taken from MSR Rex’s site: “Rex is a tool that explores .NET regexes and generates members efficiently.”

Regex is short for regular expression, a compact formal notation to describe set of character strings (words), which can be shown to have the same expressive power like formal languages and deterministic state automata (see [1], [2]).

Now, using Rex you are able to do the following:

  1. Define a regular expression (“pattern”) to which expressions/strings/words must adhere.
  2. Let Rex take care of finding instantiations (“pattern matches”) of (1).

Why would you use Rex?

Maybe to test your skills in writing regular expressions. That is, testing a regular expression which you wrote by feeding it to Rex, which in turn will generate words that match your regular expression. Seeing an instantiation you might not have thought about or which even is not permissible in your case, would lead you to refine your regular expression.

Based on the idea of testing, you might have written several string or integer handling routines. You might ask Rex to help with generating data points (words and numbers) to test your routines. Although, Rex would be perfectly able to help you here, a more powerful tool comes to mind – Pex.

But imagine you have some data logic. For example, you need to read a csv file and process its records. Now what you might want to do, is have Rex generate that file four you. For example, imagine your csv file has a structure like (first name, last name, zip, city, state) but you actually do not care whether or not first name is “Steve” or city matches with zip (5 digit number), and so on. You simply want to test if your parsing and storage routines work on correct integer and string values. In that case, you might ask Rex to help you generate, let’s say 10000 lines of data by:

rex "^[a-z]+,[a-z]+,[0-9]{5},[a-z]+,[a-z]+$" /k:10000

Rex would output all data points (e.g., "l,yt,83312,f,e") to the standard output. Since we want this output to be stored into a file (let’s say “1.csv”), we need a mechanism to either cut/paste or pipe data to a different output stream. Luckily, Rex has an option (the /file:<your file> switch) for that:

rex "^[a-z]+,[a-z]+,[0-9]{5},[a-z]+,[a-z]+" /k:10000 /file:".\1.csv"

You might want to specify regular expression generation options (see [3]) or do more elaborate regular expression exploration. In that case a peak at the various options provided by the Rex CLI-tool might be helpful.

In case you wanted to deeply integrate Rex functionality with your own application, you might want to use Rex’s object model. In that case, you could simply do:

  1. Start Visual Studio
  2. Create a new C# console project
  3. Add the Rex executable (Rex.exe) as a reference to your project
  4. Open the Rex namespace (using Rex;)
  5. Instantiate the Rex Engine choosing an encoding and a start up seed (new RexEngine(…))
  6. Ask Rex to generate strings conforming to one or more regular expressions provided by you (GenerateMembers).

Adding all of the above together, one might come to:

 using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

using Rex;

namespace RexTest
{
    class Program
    {
        [STAThread()]
        public static void Main(string[] args)
        {
            Console.WriteLine("Rex - Test");

            RexEngine engine = new RexEngine(CharacterEncoding.Unicode, (int)DateTime.Now.Ticks);

            string[] regExes = new string[]{
                "^Hello User [0-1]{4}$"
            };

            foreach (string s in engine.GenerateMembers(RegexOptions.None, 10, regExes))
            {
                Console.WriteLine(s);
            }

            Console.WriteLine("Press <any> key to exit!");
            Console.ReadLine();
        }
    }
}

 

Which would lead to 10 strings being generated all of the pattern prefix = “Hello User“, infix=” “ and postfix is a dual number of length 4 (see below).

rextest

References

[1]https://en.wikipedia.org/wiki/Regular_expression

[2]https://www.regular-expressions.info/

[3]https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regexoptions(VS.80).aspx