在SQL中删除具有起始和结束字符的重复项。

huangapple go评论55阅读模式
英文:

Remove duplicate with start and end character in sql

问题

I have a string and need to remove duplicate by select statement in ORACLE SQL.
e.g: 'apple-HenryHenry(Male)-SunnySunny(Female)-apple'
I want to resulting output would be: 'apple-Henry(Male)-Sunny(Female)-apple'
Everyone help me

SELECT REGEXP_REPLACE('apple-HenryHenry(Male)-SunnySunny(Female)-apple', '([[:alnum:]]+)()+', '') AS result
  FROM dual;
英文:

I have a string and need to remove duplicate by select statement in ORACLE SQL.
e.g: 'apple-HenryHenry(Male)-SunnySunny(Female)-apple'
I want to resulting output would be: 'apple-Henry(Male)-Sunny(Female)-apple'
Everyone help me

SELECT REGEXP_REPLACE('apple-HenryHenry(Male)-SunnySunny(Female)-apple', '([[:alnum:]]+)(\1)+', '\1') AS result
  FROM dual;

答案1

得分: 1

你的查询大部分正确,但它会从单词中移除重复的字母和重复的部分(比如 banana -> bana)。

你可以使用类似于下面这样的代码:

SELECT REGEXP_REPLACE('apple-HenryHenry(Male)-SunnySunny(Female)-apple', '(^|\\W)(\\w+)\+(\\W|$)', '\\\') AS result
FROM dual;

在这里,(^|\\W)(\\w+)\\2+(\\W|$) 匹配由两个或更多相等部分组成的,被非单词符号或字符串边界包围的单词。

这并不是完美的,因为它会修改 haha -> ha。如果可以保证重复单词的两个部分都以大写字母开头,你可以使用 (^|\\W)([A-Z]\\w*)\\2+(\\W|$) 代替。

SELECT REGEXP_REPLACE('bananabanana-HenryHenry(Male)-SunnySunny(Female)-apple', '(^|\\W)([A-Z]\\w*)\+(\\W|$)', '\\\') AS result
FROM dual;
-- 输出: bananabanana-Henry(Male)-Sunny(Female)-apple
英文:

Your query mostly correct, but it will remove double letters from words, an duplicate parts of words (like banana -> bana).

You can use something like this

SELECT REGEXP_REPLACE('apple-HenryHenry(Male)-SunnySunny(Female)-apple', '(^|\W)(\w+)\2+(\W|$)', '\1\2\3') AS result
  FROM dual;

Here (^|\W)(\w+)\2+(\W|$) matches words, surrounded by non word symbols or string boundaries, that consists of two or more equal parts.

It is not ideal, because it will modify haha -> ha. If it is guaranteed that both parts of duplicated word start with upper letter, you can use (^|\W)([A-Z]\w*)\2+(\W|$) instead.

SELECT REGEXP_REPLACE('bananabanana-HenryHenry(Male)-SunnySunny(Female)-apple', '(^|\W)([A-Z]\w*)\2+(\W|$)', '\1\2\3') AS result
  FROM dual;
-- Outputs: bananabanana-Henry(Male)-Sunny(Female)-apple

huangapple
  • 本文由 发表于 2023年4月11日 12:37:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982443.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定