Monday, October 27, 2008

Change file date using regular expression

This is probably a simple task, but it's good to have handy if you need to do any kind of string that looks like a date manipulation using regular expressions.
Lets say you have a date which is 08/25/2008 and you need to convert it to 2008-08-15. Notice I'm changing the date from 25 to 15.

declare:
using System.Text.RegularExpressions;
string filedate = "08/25/2008";
string newdate = Regex.Replace(filedate, @"^(?\d{2})/(?\d{2})/(?\d{4})$", "${year}-${month}-15");

No comments: