Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expressions: remove non alpha numerics, with an exception

To remove all non-alpha numeric chars, the regex would be

x = regexp_replace(somestring, '[^a-zA-Z0-9]+', '', 'g')

But what if I want to leave underscores untouched ?

like image 520
David Avatar asked Apr 11 '13 15:04

David


People also ask

How do you remove a non-alphanumeric character from a string?

Using Regular Expression We can use the regular expression [^a-zA-Z0-9] to identify non-alphanumeric characters in a string. Replace the regular expression [^a-zA-Z0-9] with [^a-zA-Z0-9 _] to allow spaces and underscore character.

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 replace all non-alphanumeric character with empty string provide answer using regex?

replaceAll("/[^A-Za-z0-9 ]/", ""); java. regex.

How do you remove non-alphanumeric characters in Python?

Use the isalnum() Method to Remove All Non-Alphanumeric Characters in Python String. We can use the isalnum() method to check whether a given character or string is alphanumeric or not. We can compare each character individually from a string, and if it is alphanumeric, then we combine it using the join() function.


1 Answers

Then you need to use :

x = regexp_replace(somestring, '\W+', '', 'g')

\W is the same as [^a-zA-Z0-9_]

like image 118
Oussama Jilal Avatar answered Sep 21 '22 20:09

Oussama Jilal