I am sure that has been asked before, but I cannot find the appropriate question(s).
Being new to C#'s Regex, I want to mimic what is possible e.g. with sed
and awk
where I would write s/_(20[0-9]{2})[.0-9]{1}/\1/g
in order to find obtain a 4-digit year number after 2000 which is has an underscore as prefix and a number or a dot afterwards. The \1
refers to the value within brackets.
Example: Both files fx_201902.csv
or fx_2019.csv
should give me back myYear=2019
. I was not successful with:
string myYear = Regex.Replace(Path.GetFileName(x), @"_20([0-9]{2})[.0-9]{1}", "\1")
How do I have to escape? Or is this kind of replacement not possible? If so, how would I do that?
Edit: My issue how to do the /1
in C#, in other words how to extract a regex-variable. Please forgive me my typos in the original post - I am trying the new SO app and I submitted earlier than intended.