Q:

Parsing dates with regex in C#

I'm working on a C# app, and I get a string with a date or part of a date and I need to get the day, month and year for that string. For example: 
string example='31-12-2010'
string day = Regex.Match(example, "REGULAR EXPRESSION FOR DAY").ToString();
string month = Regex.Match(example, "REGULAR EXPRESSION FOR MONTH").ToString()
string year = Regex.Match(example, "REGULAR EXPRESSION FOR YEAR").ToString()

day = "31"
month = "12"
year = "2010"

ex2: 
string example='12-2010'

string month = Regex.Match(example, "REGULAR EXPRESSION FOR MONTH").ToString()
string year = Regex.Match(example, "REGULAR EXPRESSION FOR YEAR").ToString()

month = "12"
year = "2010"

Any idea?

A:

Don't use regular expressions for that.
Instead use
DateTime temp = DateTime.Parse(example);

Now a lot of useful properties are at your disposal. temp.Day e.g.

