Friday, June 13, 2014

Search and Replace special characters in Oracle DB

Using Regular expressions we can search for special characters and replace those with desired character.
select regexp_replace(‘Search and Replace special (.,_-$%&^&) characters’, ‘[^0-9A-Za-z]‘, ‘ ‘) from dual;
Output will be
——————————————————————
Search and Replace special             characters
Special characters (.,_-$%&^&) are replaced with space as specified in the regexp_replace() function.
If we use [[:cntrl:]] character class in the syntax ,then it will remove all nonprinting characters
Note:a control character or non-printing character is a code point (a number) in a character set, that does not in itself represent a written symbol. It is in-band signaling in the context of character encoding.
ex: C0,C1, BEL (which is intended to cause an audible signal in the receiving terminal), SYN (which is a synchronization signal), and ENQ (a signal that is intended to trigger a response at the receiving end, to see if it is still present).
for more http://aravinddarla.blogspot.in/2014/06/ascii-table.html

select regexp_replace(‘Search and Replace special (.,_-$%&^&) characters’, ‘[[:cntrl:]]’) from dual;
———————————————————————————————-
Search and Replace special (.,_-$%&^&) characters

Removing HTML tags from text:

select trim(regexp_replace('<p>Test Removing</p><ul><li>HTML entities &amp; Tags</li></ul>',
'<.+?>|\&(nbsp;)|(amp;)',' '
)) from dual;
--------------------------------------------------------------------------------------------------
Test Removing   HTML entities & Tags


FYI :
Character Class                   Meaning
______________________________________________________________________________
[:alnum:]                                All alphanumeric characters
[:alpha:]                                 All alphabetic characters
[:blank:]                                 All blank space characters.
[:cntrl:]                                   All control characters (nonprinting)
[:digit:]                                   All numeric digits
[:graph:]                                All [:punct:], [:upper:], [:lower:], and [:digit:] characters.
[:lower:]                                All lowercase alphabetic characters
[:print:]                                 All printable characters
[:punct:]                                All punctuation characters
[:space:]                                All space characters (nonprinting)
[:upper:]                               All uppercase alphabetic characters
[:xdigit:]                                All valid hexadecimal characters

No comments:

Post a Comment