REGEXP_REPLACE - 从字符串中删除逗号只有在()的封闭 [英] REGEXP_REPLACE - remove commas from string ONLY if enclosed in ()'s
问题描述
我在oracle论坛网站找到一个示例:
I find an example at oracle forum site :
输入字符串: a,b,c(x,y,z) ,(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
输出: 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.
输出: 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
由@ Kobi的注释:
此正则表达式删除()之前的
,
code>
它可以扩展到9(我有一个书,说明\1 ... \500应该可以,但只有\1 ... \9工作)
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
这篇关于REGEXP_REPLACE - 从字符串中删除逗号只有在()的封闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!