David Duman
David Duman

Reputation: 6656

Regular Expression pattern issue

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

Upvotes: 2

Views: 110

Answers (3)

rullof
rullof

Reputation: 7424

With C# you don't need the slashes (/) aroud your regular expression:

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

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

Upvotes: 0

Caio Oliveira
Caio Oliveira

Reputation: 1243

This worked for me

string str = "a@4( asd1";
Regex rex = new Regex(@"[^a-zA-Z0-9]+");           
System.Console.WriteLine(rex.Replace(str, ""));

Upvotes: 4

p.s.w.g
p.s.w.g

Reputation: 149010

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

Upvotes: 5

Related Questions