Here's some code I use in a logging utility that does almost exactly what you want, although with a slightly different format:
Regex rgx = new Regex("{([Dd]:([^{]+))}");
private string GenerateTrueFilename()
{
// Figure out the new file name
string lfn = LogFileName;
while (rgx.IsMatch(lfn))
{
Match m = rgx.Matches(lfn)[0];
string tm = m.Groups[1].Value;
if (tm.StartsWith("D:"))
{
string sm = m.Groups[2].Value;
string dts = DateTime.Now.ToString(sm);
lfn = lfn.Replace(m.Groups[0].Value, dts);
}
}
return lfn;
}
The rgx defines the format, which currently really only has one format:
D:{datestring}
LogFileName is a variable defined elsewhere in code, but has a default:
LogFileName = "LogFile.{D:yyyy-MM-dd}.log"
so hopefully you can see from this how to proceed with other variables if needed.