简体   繁体   中英

REGEXP_REPLACE - remove commas from string ONLY if enclosed in ()'s

I find an example at oracle forum site :

Input string : a, b, c (x, y, z), a, (xx, yy, zz), x,

WITH t AS (SELECT 'a, b, c (x, y, z), a, (xx, yy, zz), x,' col1 
         FROM dual)
SELECT t.col1
 , REGEXP_REPLACE(t.col1, '(\(.*?\))|,', '\1') new_col
FROM t

Output : abc (x, y, z) a (xx, yy, zz) x

But i want to make opposite of that. Just remove this character , from inside () and remain outside.

Output : a, b, c (xyz), a, (xx yy zz), x,

This will work for a constant length of arguments with in the brackets.

REGEXP_REPLACE(t.col1, '(\(.*?),(.*?),(.*?\))', '\1\2\3') new_col

update inspired by @Kobi's comment:
this regular expression removes the 1st, optional 2nd and optional 3rd , between ()
it can be extended up to 9 (I've a book stating \\1 ... \\500 should be possible but only \\1 ... \\9 worked)

REGEXP_REPLACE(t.col1, '\(([^,]*),([^,]*),?([^,]*),?([^,]*)\)', '(\1\2\3\4)') new_col

Not sure if REGEXP_REPLACE supports negative look aheads and look behinds, but if it does this would work: ,(?<!\\)[^\\(]*)(?![^\\)]*\\()

I tested with C#:

string s = "a, b, c (x, y, z), a, (xx, yy, zz), x,";

Console.WriteLine(Regex.Replace(s, @",(?<!\)[^\(]*)(?![^\)]*\()", ""));

您使用的正则表达式的一些修改版本:

REGEXP_REPLACE(column_name, '((\)|^).*?(\(|$))|,', '\1')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM