gpt4 book ai didi

sql - Oracle regexp 忽略重复字符

转载 作者:行者123 更新时间:2023-12-04 04:45:58 26 4
gpt4 key购买 nike

我有一个字符串:
hheelllloo wwoorrlldd !!应该返回 hello world!
我对上述的尝试是

SELECT regexp_substr('hheelllloo wwoorrlldd !!', '.', LEVEL*2-1)l_val
FROM dual
CONNECT BY LEVEL <= LENGTH('hheelllloo wwoorrlldd !!')/2;

但它不是我需要的方式,逻辑没有正确使用。
我也试过使用 '(\w)\1'
我在示例数据中的预期结果:
WITH t AS
( SELECT 'hheelllloo wwoorrlldd!!' AS word FROM dual
UNION
SELECT 'hellow world!' FROM dual
UNION
SELECT 'ootthheerrss' FROM dual
UNION
SELECT 'ootthheeerrss' FROM dual
)
SELECT * FROM t;

输出应如下所示:
 hello world!    --expression applied
hellow world! -- not needed for non-repeated characters
others --expression applied
otheers --applied and extra `e` considered as non-repeated.

我可以在一个查询中完成整个查询,还是第一个查询?
提前致谢,这仅用于我的练习并了解不同的逻辑。

最佳答案

您可以使用 regexp_replace()带反向引用的正则表达式函数:

SQL> WITH t1(col) AS (
2 select 'hheelllloo wwoorrlldd!!' from dual union all
3 select 'hellow world!' from dual union all
4 select 'ootthheerrss' from dual union all
5 select 'ootthheeerrss' from dual
6 )
7 select regexp_replace(col, '(.)\1', '\1') as res
8 from t1
9 ;

RES
--------------
hello world!
helow world!
others
otheers

关于sql - Oracle regexp 忽略重复字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18180577/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com