简体   繁体   中英

C# regex how to exclude from string

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!!

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM