简体   繁体   中英

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, @"-");
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.

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

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