how to escape brackets in regex replace in oracle sql - sql

My query is not working as expected :
SELECT REGEXP_REPLACE('This is testing data SIL(TM)T for once ',
'SIL(TM)T', 'SIL<REFERENCE ID="8208" TYPE="trademark"/>T')as
Newdescriptiontext from dual
output should be:
This is testing data SIL<REFERENCE ID="8208" TYPE="trademark"/>T for once
which is not the case .Need guidance .

try replace instead of regexp_replace
SELECT REPLACE('This is testing data SIL(TM)T for once ',
'SIL(TM)T', 'SIL<REFERENCE ID="8208" TYPE="trademark"/>T')as
Newdescriptiontext from dual;

You simply have to escape the parentheses:
SELECT REGEXP_REPLACE('This is testing data SIL(TM)T for once ',
'SIL\(TM\)T', 'SIL<REFERENCE ID="8208" TYPE="trademark"/>T')as Newdescriptiontext
from dual
In a regexp, they are used to delimit a "subexpression", so '(TM)' matches 'TM'; if you escape them, they'll be interpreted as plain characters, thus having '\(TM\)' matching '(TM)'

Related

How do I use an ORACLE REGEX function to remove all leading and trailing line break characters and spaces?

How do I use an ORACLE REGEX function to remove all leading and trailing line break characters and spaces?
For example, assume I have the following string where refers to actual invisible carriage return line feed characters. Here's the input:
"
SELECT *
FROM
TABLE
"
And here's the desire output:
"SELECT *
FROM
TABLE"
This would do it if regex_replace() is a requirement:
select regexp_replace('
SELECT *
FROM
TABLE
', '^\s*|\s*$', '') as hello
from dual
See https://www.techonthenet.com/oracle/functions/regexp_replace.php for documentation.
A single regexp_replace is sufficient, eg.
select regexp_replace('
select frut
from prut
','^[[:space:]]*(.*[^[:space:]])[[:space:]]*$','\1',1,1,'mn') from dual;
results in
select frut
from prut

Oracle remove special characters

I have a column in a table ident_nums that contains different types of ids. I need to remove special characters(e.g. [.,/#&$-]) from that column and replace them with space; however, if the special characters are found at the beginning of the string, I need to remove it without placing a space. I tried to do it in steps; first, I removed the special characters and replaced them with space (I used
REGEXP_REPLACE) then found the records that contain spaces at the beginning of the string and tried to use the TRIM function to remove the white space, but for some reason is not working that.
Here is what I have done
Select regexp_replace(id_num, '[:(),./#*&-]', ' ') from ident_nums
This part works for me, I remove all the unwanted characters from the column, however, if the string in the column starts with a character I don't want to have space in there, I would like to remove just the character, so I tried to use the built-in function TRIM.
update ident_nums
set id_num = TRIM(id_num)
I'm getting an error ORA-01407: can't update ident_nums.id_num to NULL
Any ideas what I am doing wrong here?
It does work if I add a where clause,
update ident_nums
set id_num = TRIM(id_num) where id = 123;
but I need to update all the rows with the white space at the beginning of the string.
Any suggestions are welcome.
Or if it can be done better.
The table has millions of records.
Thank you
Regexp can be slow sometimes so if you can do it by using built-in functions - consider it.
As #Abra suggested TRIM and TRANSLATE is a good choice, but maybe you would prefer LTRIM - removes only leading spaces from string (TRIM removes both - leading and trailing character ). If you want to remove "space" you can ommit defining the trim character parameter, space is default.
select
ltrim(translate('#kdjdj:', '[:(),./#*&-]', ' '))
from dual;
select
ltrim(translate(orginal_string, 'special_characters_to_remove', ' '))
from dual;
Combination of Oracle built-in functions TRANSLATE and TRIM worked for me.
select trim(' ' from translate('#$one,$2-zero...', '#$,-.',' ')) as RESULT
from DUAL
Refer to this dbfiddle
I think trim() is the key, but if you want to keep only alpha numerics, digits, and spaces, then:
select trim(' ' from regexp_replace(col, '[^a-zA-Z0-9 ]', ' ', 1, 0))
regexp_replace() makes it possible to specify only the characters you want to keep, which could be convenient.
Thanks, everyone, It this query worked for me
update update ident_nums
set id_num = LTRIM(REGEXP_REPLACE(id_num, '[:space:]+', ' ')
where REGEXP_LIKE(id_num, '^[ ?]')
this should work for you.
SELECT id_num, length(id_num) length_old, NEW_ID_NUM, length(NEW_ID_NUM) len_NEW_ID_NUM, ltrim(NEW_ID_NUM), length(ltrim(NEW_ID_NUM)) length_after_ltrim
FROM (
SELECT id_num, regexp_replace(id_num, '[:(),./#*&-#]', ' ') NEW_ID_NUM FROM
(
SELECT '1234$%45' as id_num from dual UNION
SELECT '#SHARMA' as id_num from dual UNION
SELECT 'JACK TEST' as id_num from dual UNION
SELECT 'XYZ#$' as id_num from dual UNION
SELECT '#ABCDE()' as id_num from dual -- THe 1st character is space
)
)

Replacing Special and Unicode Char in PostgresSQL

I am using PostgreSQL. Data in the table looks like below there is ''. I would like to remove '' with the empty value.
Query:
select 'Patriots Colony Family Monthly'
Actual Result Screenshot:
Expected result:
Abcd
You can use regex_replace to remove non alphanumeric chars
select regexp_replace(yourColumn, '[^[:alnum:]]', ' ', 'g')
from yourTable;
You can also do like this
select regexp_replace(yourColumn, '[^a-zA-Z0-9]+',' ','g')
from yourTable;

In Oracle SQL how to replace multiple char with multiple values?

My string is - ABC Corp., NY., ("Lender") As Agency
I need to replace comma with ~ and double quotes with ^.
Required Output - ABC Corp.~ NY.~ (^Lender^) As Agency
How do I do it in Oracle 11g SQL using regexp_replace()?? Or is there any other way?
The "other way" is to just do two normal replaces
select
REPLACE(REPLACE('ABC Corp., NY., ("Lender")', ',', '~'), '"', '^')
from dual
Or a TRANSLATE, which is easiest if you only need to switch single characters.
select
TRANSLATE('ABC Corp., NY., ("Lender")', ',"', '~^')
from dual
this will work:
select regexp_replace(regexp_replace(yourcolumn,',','~'),'"','^') from yourtable;

Delete certain character based on the preceding or succeeding character - ORACLE

I have used REPLACE function in order to delete email addresses from hundreds of records. However, as it is known, the semicolon is the separator, usually between each email address and anther. The problem is, there are a lot of semicolons left randomly.
For example: the field:
123#hotmail.com;456#yahoo.com;789#gmail.com;xyz#msn.com
Let's say that after I deleted two email addresses, the field content became like:
;456#yahoo.com;789#gmail.com;
I need to clean these fields from these extra undesired semicolons to be like
456#yahoo.com;789#gmail.com
For double semicolons I have used REPLACE as well by replacing each ;; with ;
Is there anyway to delete any semicolon that is not preceded or following by any character?
If you only need to replace semicolons at the start or end of the string, using a regular expression with the anchor '^' (beginning of string) / '$' (end of string) should achieve what you want:
with v_data as (
select '123#hotmail.com;456#yahoo.com;789#gmail.com;xyz#msn.com' value
from dual union all
select ';456#yahoo.com;789#gmail.com;' value from dual
)
select
value,
regexp_replace(regexp_replace(value, '^;', ''), ';$', '') as normalized_value
from v_data
If you also need to replace stray semicolons from the middle of the string, you'll probably need regexes with lookahead/lookbehind.
You remove leading and trailing characters with TRIM:
select trim(both ';' from ';456#yahoo.com;;;789#gmail.com;') from dual;
To replace multiple characters with only one occurrence use REGEXP_REPLACE:
select regexp_replace(';456#yahoo.com;;;789#gmail.com;', ';+', ';') from dual;
Both methods combined:
select regexp_replace( trim(both ';' from ';456#yahoo.com;;;789#gmail.com;'), ';+', ';' ) from dual;
regular expression replace can help
select regexp_replace('123#hotmail.com;456#yahoo.com;;456#yahoo.com;;789#gmail.com',
'456#yahoo.com(;)+') as result from dual;
Output:
| RESULT |
|-------------------------------|
| 123#hotmail.com;789#gmail.com |