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 ?
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.
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.
replaceAll("/[^A-Za-z0-9 ]/", ""); java. regex.
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.
Then you need to use :
x = regexp_replace(somestring, '\W+', '', 'g')
\W
is the same as [^a-zA-Z0-9_]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With