Thursday, July 22, 2010

Some useful Regex Tips and Tricks

So, I’ve never been a wiz at Regex, but I’ve found myself using it more and more lately for projects at work.  The most recent case was when I had to validate some time data in a text field, to ensure it was in proper 12 hour format with an AM/PM suffix.  There was an existing method to do that validation in our code base, but it was hard to follow and it was returning false positives if you passed in plain integer values.  So, instead of trying to fix the malfunctioning code, I simply wrote a new validator that was based on a proven Regular Expression.  At the end of the day, my RegEx validator worked like a charm, and it was super easy to use.  Here’s the entire ValidateTime() Method:
   1: /// <summary>
   2: /// Validates the time against a regular expression that ensures standard military or 
   3: /// standard AM/PM time formats. Returns true or false based on the input time span.
   4: /// </summary>
   5: /// <param name="timeSpan"></param>
   6: /// <returns></returns>
   7: public static bool ValidateTime(this TimeSpan timeSpan)
   8: {
   9:     bool validTime = false;
  10:     string tmpTime = timeSpan.ToString();
  11:  
  12:     Regex validTimePattern = new Regex(@"^((([0]?[1-9]|1[0-2])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?( )?(AM|am|aM|Am|PM|pm|pM|Pm))|(([0]?[0-9]|1[0-9]|2[0-3])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?))$");
  13:  
  14:     validTime = validTimePattern.IsMatch(tmpTime);
  15:     return validTime;
  16: }

That’s the joy of RegEx.  The old method was 50+ lines of code and it wasn’t reliable.  Of course, you have to be familiar with RegEx in order to make a method like this work… or do you…?

Well, it turns out, you really don’t.  I would be hard pressed to write a regular expression that was more complicated than, say, a simple 7 digit phone number.  I certainly didn’t have the wherewithal to write the above monstrosity of RegEx.  So, where did it come from?  The answer is hardly surprising.  I’ve had need of regular expressions before, and inevitably I always wind up going to the following site to get them:

RegEx Library

This place has a searchable interface, good categorization of expressions, and thousands upon thousands of regular expressions to choose from.  Many times there are dozens of expressions that perform the same checks, or perform very similar checks.  Unfortunately, that can cause trouble if you aren’t careful, so it’s always a good idea to double check your regular expressions before you put them into production.  I do that with the help of this site:

JavaScript Regular Expression Editor

That site is a life saver.  It’s very simple to use:  Simply enter your Regular Expression in the top box, enter the string you wish to validate in the middle box, and observe your results in the bottom box.  With a few minutes on this site, you’ll have a pretty good idea if your expression will work for you or not.  Remember to test for things like case, length, and invalid characters.  Once you have a regular expression that you are confident will work for your situation, you can use the code shown above to craft a very simple check to validate your input.

Rich

Wednesday, July 7, 2010

Home Server Setup

Oooooh, something useful to put on my Developer blog!  Awesome!

So, I just moved my Dell PowerEdge 700 into the living room last night, hiding it behind my overly massive TV.  I long ago set it up to be a personal development server, but I abandoned the project in frustration last summer when I couldn't get a wireless card to work with it and Windows Server 2008.  I don't have enough power or space near the rest of my computers to leave it in proximity to the router, so I just shut the machine down and moved on without it.

Well, a few months back I bought a new pair of Sony Home Electronics (HDTV and BluRay player), both of which are network ready.  Neither had wifi built in, but they had ethernet jacks.  So, I got a 2 port Netgear wireless N bridge.  I've had it plugged into the TV and BluRay player up until now.  However, I never really use the internet features on the TV, because I don't have sound running from the TV to the surround sound.  I just use the BluRay player for all my network streaming, and it works out just fine.

I've begun work on a personal project, building a website for a local organization.  I need, at a minimum, a place to store my code (i.e. VSS at least, TFS at best).  Since the Dell already had Windows Server 2008 and SQL Server 2008 loaded up on it, and it was just sitting there anyway, I went ahead and moved it to the alcove behind the TV, fired it up, ran windows update, and got RDP working on it.  While that was going on, I downloaded VSS and TFS from my MSDNAA account, and I'm going to see about setting those services up over the next week or so.

So, hopefully I'll be able to document my TFS setup process here.  I'm not real sure what all TFS is supposed to do, and i'm sure it's way overkill for 1 man projects, but being familiar with it will be a valuable skill at my office, which is in the process of rolling TFS into production this year.  Having VSS for the interim while I work on the TFS install will be helpful as well.

Rich