Cecil Theodore
Cecil Theodore

Reputation: 9939

Replacing specified characters using Regex .net

I have a small Regex here where I am removing all the white spaces from within a file and replacing them with '-'.

I want to also replace other characters with '-' such as ',' and '_'.

How can I list these characters in my regex?

Regex r = new Regex(@"\s+");

string fileName = r.Replace(Files.Name, @"-");

Upvotes: 1

Views: 1698

Answers (2)

xanatos
xanatos

Reputation: 111820

Regex r = new Regex(@"[\s,_-]+");

string fileName = r.Replace(Files.Name, @"-");

Be aware that the - must be the first, the last or you'll need escaping.

Upvotes: 4

Michael Low
Michael Low

Reputation: 24506

Regex r = new Regex(@"(\s|-|,|_)+");

Upvotes: 3

Related Questions