Bookmark and Share

Generate Matches for Regular Expressions Using Rex

by KodefuGuru 3. May 2010 18:59

rexRegular expressions are one of the more cryptic tools developers utilize in every day work. Sure, just about anyone understands that ^\d\d$ matches a line containing exactly two digits, but you may need a cheat sheet to figure out a regex like \b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b.

If you’re like me, you typically know that you need a regex to match on something, then you go about making a  regex to match the pattern. Then, you’ve either built a unit test or you use a website to test your regex against some test data. This is great for positive verification, but how can you be sure that bad data isn’t matched as well?

Microsoft Research has produced a tool that will explores a regex to generate matching values much like Pex explores a program to generate relevant test inputs. This tool is call Rex (Regular Expression Exploration). This solves the problem mentioned above. With Rex, you can explore valid values for a given regex and an analyze them for potential problems.

I downloaded the 1.0 release of this command-line tool and gave it a shot. It was pretty easy to get started: just type rex.exe <regex> and it will give you a value. If you want more values, type in /k:<number> (I have no idea what k stands for, it’s the only option without a full name). You can pass more than one regex in by using a file, and then with the /intersect option you can instruct rex that all regexes must be applied for the generated values. Use /? to see more options.

After trying out the regex from the beginning of this article, I discovered that Rex does have a few limitations. Here are the regex constructs not supported: anchors \G, \b, \B, named groups, lookahead, lookbehind, as-few-times-as-possible quantifiers, backreferences, conditional alternation, substitution.

I’m not satisfied with a command-line tool. Luckily, Rex is a .NET application. I created a new Console application and added a reference to Rex.exe. I then added a using clause to Rex, and began typing out some code. Here’s what I came up.

string regex = @"^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$";
RexSettings settings = new RexSettings(regex) { k = 5, encoding = CharacterEncoding.ASCII };
var results = RexEngine.GenerateMembers(settings);
foreach (var result in results) { Console.WriteLine(result); }

It turned out that you can create an instance of RexEngine and GenerateMembers, but since it was meant to be a command-line tool I decided to use the RexSettings class and pass it to the static version of the GenerateMembers method. I obtained that particular regex from a site claiming that it matched dates. Well it does, but not consistently. As you can see by Rex’ output, it would be better to come up with a different regex, enforce consistency on the separator, or make it match one of several regexes.

12/09-2085
12-31-2089
09 31.2098
12/15-1989
03 31 1989

I vote for consistency on the separator.

This is a very useful tool Microsoft Research has invented. My only concern is that unscrupulous people will be using it as well. Then again, they’ve probably had these kind of generators all along.

Tags: ,

Weapons

Bookmark and Share

Community Office Templates

by KodefuGuru 11. November 2009 13:21

I rarely visit Microsoft Office Online to download templates because they’re usually quite bland. However, I’m a programmer (not a designer) so I’m forced to scour the Internet in search of templates to use because I would spend an inordinate amount of time arranging things in PowerPoint or Word.

I’m not exactly sure of the timeframe, but something has changed on Microsoft’s site. There are now tons of great templates! Microsoft opened up a Community section and allowed people to start submitting their own. This is a live saver for non-designers like myself!

Bookmark and Share

Microsoft XNA Game Studio 3.1 Zune Extensions

by KodefuGuru 15. September 2009 18:03

Microsoft has released extensions to the XNA Game Studio 3.1 for the Zune HD. The add-on will allow you to take advantage of the Touch APIs and the new Accelerometer. Included in the download are documentation and examples on how to leverage the new APIs.

Bookmark and Share

SQL Server 2008 R2 CTP2

by KodefuGuru 11. August 2009 10:22

SQL Server 2008 R2 CTP2 is availabe for download for MSDN subscribers! Non-subscribers can get it on August 12th.

New features in this CTP:

Application & Multi-Server Management
Report Builder 3.0
Scale Up with support for up to 256 logical processors

Tags:

Weapons

Bookmark and Share

It's All About The Tools

by KodefuGuru 12. April 2009 16:21

I had the pleasure of being in the audience for Russ Fustino's "It's All About The Tools" recording at the South Florida Code Camp. Here's the video from that session.

Get Microsoft Silverlight

Bookmark and Share

SharePoint Designer Free

by KodefuGuru 2. April 2009 10:05

SharePoint Designer 2007 is now available as a free download!

This can be disappointing to someone who has already purchased it, and it's disappointing to me as a user group leader who has a copy to give away. However, if you have SharePoint Designer 2007 Software Assurance current as of April 1st, 2009, you will receive upgrade rights to Expression Web for the lifetime of their SA agreement.

Bookmark and Share

IE8 Released

by KodefuGuru 20. March 2009 17:39

IE8 has been officially released and the download is available.

So why should you download IE8? Well, out of all the new features, accelerators are the best! No, it isn't some trick to load web pages faster. Instead, accelerators accelerate your own web browsing experience. For example, just yesterday, I had to find the meeting location where I was giving a presentation. I also need to pull up directions. After I found the address, here's what I would have to do in other browsers to get directions:

Click 1: highlight and select the address information
Click 2: copy that information (ctrl-c)
Click 3: open a new tab or window
Click 4: enter maps.live.com in the address bar
Click 5: paste the address information into that new site
Click 6: enter, and then view the results

Here's what I do in IE8:

Click 1: highlight and select the address information
Click 2: right-click and hover over "Map with LiveEarth" accelerator, click on directions.

This definitely accelerates my browsing experience.

Bookmark and Share

Architecture Journal Reader (Beta)

by KodefuGuru 10. December 2007 16:04
A beta of the Architecture Journal Reader has been released. Use it to get all the latest, greatest architecure articles from Ray Ozzie, the Chief Software Architect of Microsoft.
Bookmark and Share

Visual Studio 2008 Tip of the Day Sidebar Gadget

by KodefuGuru 2. December 2007 18:51
Get daily tips on Visual Studio 2008 in this recently released free gadget.
Bookmark and Share

SyncToy 2.0 Beta

by KodefuGuru 24. October 2007 17:57

Microsoft has made improvements on SyncToy and released the beta for public consumption. Like the name suggests, SyncToy lets you keep your files syncronized between folders and computers.

Here are the new features for the 2.0 beta release:

  • Dynamic Drive Letter Assignment: Drive letter reassignment will now be detected and updated in the folder pair definition.
  • True Folder Sync: Folder creates, renames and deletes are now synchronized for all SyncToy actions.
  • Exclusion Filtering Based on Name: File exclusion based on name with exact or fuzzy matching.
  • Filtering Based on File Attributes: The ability to exclude files based on one or more file attributes (Read-Only, System, Hidden).
  • Unattended Folder Pair Execution: Addressed issues related to running scheduled folder pairs while logged off.
  • Folder Pairs With Shared Endpoints: Ability for folder pairs associated with the same or different instances of SyncToy to share end-points.
  • Command line enhancements: Added the ability to manage folder pairs via the command line interface.
  • The SyncToy engine has been rearchitected to provide scalability and the ability to add significant enhancements in future releases.
  • Sync engine is also more robust insomuch that many single, file level errors are skipped without affecting the entire sync operation.
  • Sync Encrypted Files: Sync of Encrypted files works when local folder and files are encrypted, which addresses the common scenario involving sync between local, encrypted laptop PC folder and remote, unencrypted desktop PC folder.
  • 64-Bit compatibility
  • Folder pair rename
  • Sub-folder Exclusion Enhancements: Descendents creates under excluded sub-folders are automatically excluded.
  • Folder Pair Metadata Moved: Folder pair metadata removed from MyDocuments to resolve any issues with server-based folder pair re-direction setup.
  • Removed combine and subscribe actions.
  • KodefuGuru.GetInfo()

    Chris Eargle
    LinkedIn Twitter Technorati Facebook

    Chris Eargle
    C# MVP, INETA Community Champion


    MVP - Visual C#

     

    INETA Community Champions
    Friend of RedGate
    Telerik .NET Ninja
    Community blogs & blog posts

    I am a #52er

    I have joined Anti-IF Campaign


    World Map

    Tag cloud

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

    © Copyright 2010