Im working on regex on c#, and I need to get only a part from the string and exclude all the rest.
var file = "XD_ADX_W_CGX_CLAIMS_2021_07_11.TXT";
I want to only get "CGX_CLAIMS" from above string, XD_ADX is variable, W is static and 2021_07_11 is also variable
I was able to extract the date with the below regex:
Regex r = new Regex(@"[_]\d{4}[_]\d{2}[_]\d{2}[.]", RegexOptions.RightToLeft);
Now I need to extract only CGX_CLAIMS (that is variable.. could be other string)... any idea?
Thank you!!
CodePudding user response:
Since _W_
is static, you can use
_W_(.*?)_\d{4}_\d{2}_\d{2}\.
See the regex demo. Details:
_W_
- a known substring(.*?)
- Group 1: any zero or more chars other than a line feed char, as few as possible_\d{4}_\d{2}_\d{2}\.
-_
, four digits,_
, two digits,_
, two digits and a.
char.
In C#, you can use
var output = Regex.Match(text, @"_W_(.*?)_\d{4}_\d{2}_\d{2}\.")?.Groups[1].Value;