简体   繁体   中英

split and get value after string in consolidated value using c#

I want to get the value from the List<string> which contains different format as below.

Formats:

var fieldValue =  { 
      "00 1234567 89 EXT 098", 
      "001 234 56 789 EX 0987",  
      "0 0123456789 EXTEN 09",
      "00123456789 EXTENSION0987",
      "00123456789E098987" 
}

Among all these values, I want to get the value ('098', '0987', '09', '0987', '098987') after string such as 'EXT', 'EX', 'EXTEN', 'EXTENSION', 'E'

The string may be differ in each value, so that I confused to apply condition to get the values after that.

Also I am unable to split this using <space> since it may contain in some values.

Please advice me to get the required values. Thanks in advance,

We can try doing a regex replacement on ^.*?(?=\\d+$) and replace with empty string:

string input = "00123456789E098987";
string output = Regex.Replace(input, "^.*?(?=\\d+$)", "");
Console.WriteLine(input);
Console.WriteLine(output);

This prints:

00123456789E098987
098987

Note: In the case of an input which does not end in at least one digit, the above logic would just return that original input. Ie 123ABC remains as that.

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