Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Remove repeating words in string using regexp

Sanjeev ChauhanJan 5 2017 — edited Jan 13 2017

I have an ordered list of words separated by single space. I did like to return only distinct words from the list. For e.g.

1)  "a aa aa b c"  --> "a aa b c"

2)  "a b c c c"  --> "a b c"

I have

with

  src as (

    select 'a aa aa b c' as str from dual union all

    select 'a b c c c' as str from dual

  )

select regexp_replace(str, '([^ ]+)( \1)+','\1') regexp1

     , regexp_replace(str, '([^ ]+ )\1+','\1') regexp2

     , regexp_replace(rtrim(str)||' ', '([^ ]+ )(\1)+','\1') regexp3

from src

/

column regexp1 gets 2) right not 1)

column regexp2 gets 1) right not 2)

column regexp3 gets both right but, can this be done without appending an extra space

This post has been answered by mathguy on Jan 5 2017
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 10 2017
Added on Jan 5 2017
24 comments
11,911 views