user9176988
user9176988

Reputation:

regex for removing characters at end of string

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

Upvotes: 4

Views: 21482

Answers (2)

Chris
Chris

Reputation: 27599

[ ,;:/]*$ 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.

Upvotes: 9

Shiran Dror
Shiran Dror

Reputation: 1480

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

string line = "some text :  ;  ,    / " 
char[] charsToTrim = {',', ':', ';', ' ', '/'};
string trimmedLine = line.TrimEnd(charsToTrim);

Upvotes: 3

Related Questions