I want to replace patterns of date in string as date, so from this input:
log_{yyyy-MM} foo {HH}.txt
I should get:
log_2020-06 foo 11.txt
I created something like this:
public static string ReplaceDate(string input)
{
var r = new Regex(@"\{(.*?)\}");
var dateFormat = r.Match(input).Groups[1].Value; //Get the first matching group
return r.Replace(input,DateTime.Now.ToString(dateFormat));
}
And with this input:
log_{yyyy-MM} foo.txt
My code returns (which is OK):
log_2022-06 foo.txt
but with this input:
log_{yyyy-MM} foo {HH}.txt
I get wrong output:
log_2022-06 foo 2022-06.txt
Any ideas how can I make it working with many groups?