How to Remove All Non Alphanumeric Characters from a String Except Dash

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

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

Regex rgx = new Regex("[^a-zA-Z0-9 -]");
str = rgx.Replace(str, "");

Replace non alphanumeric characters except some exceptions python

You can specify everything that you need not remove in the negated character clas.

re.sub(r'[^\w'+removelist+']', '',mystring)

Test

>>> import re
>>> removelist = "=."
>>> mystring = "asdf1234=.!@#$"
>>> re.sub(r'[^\w'+removelist+']', '',mystring)
'asdf1234=.'

Here the removelist variable is a string which contains the list of all characters you need to exclude from the removal.

What does negated character class means

When the ^ is moved into the character class it does not acts as an anchor where as it negates the character class.

That is ^ in inside a character class say like [^abc] it negates the meaning of the character class.

For example [abc] will match a b or c where as [^abc] will not match a b or c. Which can also be phrased as anything other than a b or c

How can I remove all non-alphanumeric characters from a string, except for '#', with regex?

In order to avoid removing the hash symbol, you need to add it into the negated character class:

r'[^A-Za-z0-9#]+'
^

See the regex demo

Remove non numeric characters except dash

I'd suggest:

var stripped = string.replace(/[^0-9\-]/g,'');

JS Fiddle demo.

^ in the character class (within the [ and ]) is the NOT operator, so it matches characters which are not 0-9 or the (escaped) - character.

As noted in the comment to this answer, by Ted Hopp, it's not necessary to escape the - when it's the last character, but I habitually do so in order to save having to remember that proviso.

References:

  • JavaScript Regular Expressions.

Remove all the characters and special characters except underscores, dashes and numbers

This is way easier with a negated character class:

str.replace(/[^0-9_-]/g, '');

Everything that is not a digit between 0 and 9, an underscore or a minus, will get replaced by an empty string.

(The first - means “range” here, because it is between two other characters, the second one just means “itself”, because it is at the end of the character class. If it was placed somewhere other than the very start or end, it would need to be escaped, \-.)

How to remove non-alphanumeric characters?

Sounds like you almost knew what you wanted to do already, you basically defined it as a regex.

preg_replace("/[^A-Za-z0-9 ]/", '', $string);

how to strip all characters except for alphanumeric and underscore and dash?

Yes, but it can be optimised slightly:

preg_replace('/[^\w-]/', '', $string);

\w matches alphanumeric characters and underscores. This has the added advantage of allowing accented characters if your locale allows.



Related Topics



Leave a reply



Submit