Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex for removing characters at end of string

Tags:

c#

regex

i would like to match recursively, all text that ends with : or / or ; or , and remove all these characters, along with any spaces left behind, in the end of the text.

Example:

some text :  ;  ,    / 

should become:

some text

What i have tried, just removes the first occurrence of any of these special characters found, how one can do this recursively, so as to delete all characters found that match? regex i use:

find: [ ,;:/]*

replace with nothing


2 Answers

[ ,;:/]*$ should be what you need. This is the same as your current regex except with the $ on the end. The $ tells it that the match must happen at the end of the string.

like image 69
Chris Avatar answered Oct 23 '25 02:10

Chris


You can use C#'s TrimEnd() like so

string line = "some text :  ;  ,    / " 
char[] charsToTrim = {',', ':', ';', ' ', '/'};
string trimmedLine = line.TrimEnd(charsToTrim);
like image 36
Shiran Dror Avatar answered Oct 23 '25 01:10

Shiran Dror



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!