User987 User987 - 3 months ago 16
ASP.NET (C#) Question

Regex to replace special characters with specific sign

I'm trying to use Regex to change my URL from this:

http://localhost:51577/Item/92MM+BLACK+CASE+FAN+W%2f+3+PIN+CONNECTOR+-+Cool+%26+Quiet/222069843383


Into a URL that would look like this:

http://localhost:51577/Item/92MM-BLACK-CASE-FAN-W-2f-3-PIN-CONNECTOR-Cool-26-Quiet/222069843383


Any
%
,
+
or
+-+
sign would be replaced with
-
sign using regex. I think regex is the best solution for this, but I'm not so familiar with writing regex expressions... Can someone help me out with this?

Edit: Guys I have an even better idea... I have the Title name in controller in following format:

92MM BLACK CASE FAN W/ 3 PIN CONNECTOR - Cool & Quiet


How could I write an regex to replace white spaces and remove any extra white space (if there are any) in the string array...

Edit 2: Basically replacing any special character with a
-
sign... Any ideas?

Answer

https://msdn.microsoft.com/en-us/library/e7f5w83z(v=vs.110).aspx

Regex.Replace(YOUR_STRING, "[^0-9a-zA-Z]+", "-");

Try that

Comments