Problem statement:
- There is a path that contains a date in a specific format which is a configuration parameter. Say, “yyyy_MMM_d”,
- I need to detect if the path contains such a date:
- if it does, change it to a new date.
- if it doesn’t, append the new date to the path
- The existing date in the path could be any date, it’s not always yesterday’s or tomorrow’s.
Attempted solution:
- Detect via Regex (in the example
\d{4}_[a-zA-Z]{3}_\d{1,2}) the portion that resembles the date and validate it afterwards usingTryParseExact()- This works for a constant format, but I’d need to write a format to regexp compiler to support when the parameter changes. This isn’t really worth the effort.
So, any other solution? I think that if there’s no better way than what I have, I could probably demand a regex parameter next to the date format parameter 🙂
Split the string on the slash delimiters. In a while loop, have a check that determines if it is a valid datetime string. If not, go to the next piece. If you get through every piece, you have no date, so append one.
Note, I’m not a .net developer, and as such, this could be something horrible and nasty that should never see the light of day…