Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove all non alphanumeric characters from a string except dash?

Tags:

c#

regex

How do I remove all non alphanumeric characters from a string except dash and space characters?

like image 530
Luke101 Avatar asked Jul 09 '10 06:07

Luke101


People also ask

How do I remove all non-alphanumeric characters from a string?

A common solution to remove all non-alphanumeric characters from a String is with regular expressions. The idea is to use the regular expression [^A-Za-z0-9] to retain only alphanumeric characters in the string. You can also use [^\w] regular expression, which is equivalent to [^a-zA-Z_0-9] .

How do you remove all non letters from a string?

To remove all non-alphanumeric characters from a string, call the replace() method, passing it a regular expression that matches all non-alphanumeric characters as the first parameter and an empty string as the second. The replace method returns a new string with all matches replaced.

How do you get rid of non-alphanumeric?

Non-alphanumeric characters can be remove by using preg_replace() function. This function perform regular expression search and replace. The function preg_replace() searches for string specified by pattern and replaces pattern with replacement if found.

How do you remove all non-alphanumeric characters from a string excel?

Select the range that you need to remove non-alphanumeric characters from, and click Kutools > Text > Remove Characters. 2. Then a Delete Characters dialog box will appear, only check Non-alphanumeric option, and click the Ok button. Now all of the non-alphanumeric characters have been deleted from the text strings.


1 Answers

Replace [^a-zA-Z0-9 -] with an empty string.

Regex rgx = new Regex("[^a-zA-Z0-9 -]"); str = rgx.Replace(str, ""); 
like image 72
Amarghosh Avatar answered Sep 27 '22 19:09

Amarghosh