David
David

Reputation: 1061

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 ?

Upvotes: 18

Views: 18425

Answers (2)

Oussama Jilal
Oussama Jilal

Reputation: 7739

Then you need to use :

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

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

Upvotes: 27

James Kyburz
James Kyburz

Reputation: 14453

How about using '\W+' that replaces all non a-z and 0-9 leaving _ alone

So

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

Upvotes: 2

Related Questions