title: Parse Multiple Date Formats using RegEx in C#
body: <p>Using C# 4.5 I have a need to be able to accept dates in multiple string formats and need to be able to parse them them all into valid dates.  Examples include: </p>
  
  <pre><code>04-2014
  April, 2014
  April,2014
  </code></pre>
  
  <p>I have come up with the following code that allows me to configure a dictionary with all the possible formats with their representative RegEx format and the .NET equivalent for <code>DateTime.ParseExact</code>.  This solution works... however, there's a lot of <code>foreach</code> and <code>if</code> blocks and I just want to find out if there is a more elegant/clean/faster solution.</p>
  
  <pre><code>DateTime actualDate;
  var dateFormats = new Dictionary&lt;string, string&gt; { { @"\d{2}-\d{4}", "MM-yyyy" }, { @"(\w)+,\s\d{4}", "MMMM, yyyy" }, { @"(\w)+,\d{4}", "MMMM,yyyy" } };
  var dateValues = new[] { "04-2014", "April, 2014", "April,2014", "Invalid Date" };
  var successfulDateParse = false;
  foreach (var dateValue in dateValues)
  {
      foreach (var dateFormat in dateFormats)
      {
          var regex = new Regex(dateFormat.Key);
          var match = regex.Match(dateValue);
          if (match.Success)
          {
              actualDate = DateTime.ParseExact(match.Value, dateFormat.Value, CultureInfo.InvariantCulture);
              successfulDateParse = true;
              break;
          }
      }
      if (!successfulDateParse)
      {
          // Handle where the dateValue can't be parsed
      }
      // Do something with actualDate
  }
  </code></pre>
  
  <p>Any input is appreciated!</p>