regexp_replace to remove special characters

PHOTO EMBED

Mon Mar 21 2022 02:42:50 GMT+0000 (Coordinated Universal Time)

Saved by @Bambibo9799

Syntax: REGEXP_REPLACE(source, pattern, replacement_string, [, flags])


select *, regexp_replace(t1, '[[:digit:]]+', '','g'),--remove all number in t1
regexp_replace(t2, '[[:alpha:]]+', '','g')as t3-- remove all alpahbets in t2
from
(
select *, 
regexp_replace(name, '[^a-zA-Z0-9]+', '','g')as t1 -- remove all special lcharacter
,regexp_replace(number, '[^a-zA-Z0-9]+', '','g') as t2 --remove all special character
from remove_specialcharacter_csv as rsc 
) tt

regexp_replace(t1, '\D', '','g')as t3, -- remove all letter in t1
regexp_replace(t2, '\d', '','g')as t5-- remove all number in t2


\d	matches any digit, like [[:digit:]] -- nomor 1-9
\s	matches any whitespace character, like [[:space:]]
\w	matches any word character, like [[:word:]]
\D	matches any non-digit, like [^[:digit:]] --- bukan nomor
\S	matches any non-whitespace character, like [^[:space:]]
\W	matches any non-word character, like [^[:word:]]

^	matches at the beginning of the string
$	matches at the end of the string
we have used Regular Expression in PostgreSQL using the TILDE (~) operator and the wildcard ‘.*’.
the expression can be implemented as “^.*$”.

                                                               
                                                               
SELECT * FROM GreaterManchesterCrime WHERE CrimeID ~ ‘^[A-Z].*$’ --A 1 UPPERCASE STRING
SELECT * FROM GreaterManchesterCrime WHERE CrimeID ~ ‘^[0–9].*$’ --0 1 NUMBER

SELECT * FROM GreaterManchesterCrime WHERE CrimeID ~ ‘^[A-Z] [A-Z].*$’ -- 2 UPPERCASE STRING
OR
SELECT * FROM GreaterManchesterCrime WHERE CrimeID ~ ‘^[A-Z] {2}.*$’ -- 2 UPPERCASE STRING

SELECT * FROM GreaterManchesterCrime WHERE CrimeID ~ ‘^[0–9]{2}.*$’ --2 NUMBER
SELECT * FROM GreaterManchesterCrime WHERE CrimeID ~ ‘^[A-Z][0–9].*$’ --  1 UPPERCASE 1 NUMBER
[A-Z][A-Z]\d\d = 2 UPPERCASE LETTER AND 2 DIGIT STRING
meaning only 2 UPPER followed by 2 digit can show up

SELECT REGEXP_REPLACE('ABC12345xyz', '[[:alpha:]]', '', 'g'); -- menghilangkan string
SELECT REGEXP_REPLACE('ABC12345xyz', '\D', '', 'g'); -- menghilangkan string

SELECT REGEXP_REPLACE('ABC12345xyz', '[[:digit:]]', '', 'g'); -- menghilangkan number
SELECT REGEXP_REPLACE('ABC12345xyz', '\d', '', 'g'); -- menghilangkan number

select regexp_replace(replace('dadjkasdkjad21312....','.',''),'[a-z]','','g')
content_copyCOPY