Get string after the last comma or the last number using Regex in C#

You can use

[^,\d\s][^,\d]*$

See the regex demo (and a .NET regex demo).

Details

  • [^,\d\s] – any char but a comma, digit and whitespace
  • [^,\d]* – any char but a comma and digit
  • $ – end of string.

In C#, you may also tell the regex engine to search for the match from the end of the string with the RegexOptions.RightToLeft option (to make regex matching more efficient. although it might not be necessary in this case if the input strings are short):

var output = Regex.Match(text, @"[^,\d\s][^,\d]*$", RegexOptions.RightToLeft)?.Value;

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top