Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression pattern issue

Tags:

c#

regex

asp.net

I want to delete all the characters other than letter and number from a given string. I used the pattern below but it still returns string without any change.

Regex rex = new Regex("/[^a-zA-Z0-9]+/");

Response.Write(rex.Replace("asd123!-<>@;',.", ""));

It suppose to return "asd123"

Regex patterns are like alien language to me and I dont know how to fix this.

Thanks

like image 324
dvdmn Avatar asked Jun 04 '26 13:06

dvdmn


1 Answers

In C#, you don't need to delimit regex patterns with / characters.

Try this:

Regex rex = new Regex("[^a-zA-Z0-9]+");
Response.Write(rex.Replace("asd123!-<>@;',.", ""));
like image 133
p.s.w.g Avatar answered Jun 07 '26 03:06

p.s.w.g



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!