Use C#’s regular expression library to Convert TimeSpan and DateTime to and from strings. (Named capturing groups!)

Well that title is a mouthful.  Long story short, as an exercise in regular expressions I wrote a utility class to convert strings to TimeSpans and DateTimes in C#.  This made great usage of named capturing groups.  (The download to the source code is at the bottom of this post.)

Here is the DateTime regular expression.  Note that when I new the regular expression object, I set whitespace and casing ignoring.
image

Named capturing groups allows code like this:
image

In the end, the utility class allows code like this:
image

fun!  Download all of the code here.

Have any suggestions about the code?  Any neat C# features I should have used?  Comment below!

5 thoughts on “Use C#’s regular expression library to Convert TimeSpan and DateTime to and from strings. (Named capturing groups!)

  1. Not a C# feature, but I really like the Cocoa NSDate classes, specifically this constructor. It’s discouraged mainly because it’s English only, but if you limit yourself to that, it’s a really powerful method.

  2. I’m not that much of a online reader to be honest but your sites really nice, keep
    it up! I’ll go ahead and bookmark your website to come back down the
    road. Cheers

  3. It’s hard to find your blog in google. I found it on 13 spot, you should build quality backlinks , it
    will help you to increase traffic. I know how to help you,
    just search in google – k2 seo tips

  4. I read a lot of interesting articles here. Probably you spend a lot of time writing, i know how to save you a lot of time, there is an online tool that creates unique,
    SEO friendly articles in seconds, just search in google – laranitas free content source

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>