Solved

How to use regexp_replace to replace mutiple occurance of substring?

Posted on 2013-06-03
6
1,743 Views
Last Modified: 2013-06-03
hi,
would you know if i can use regexp_replace to replace multiple occurances of a substring with in a string, with a single occurance of the string?
i have a string like so..
'XP78380;XT42564;ZX03942;XP78380;XT42564;'
i want to remove duplicate occurance of 'XP78380' and 'XT42564' and have only one occurance.
i wrote a small pl/sql block, but want to check if this can be done with a single regexp_replace?
0
Comment
Question by:Rao_S
  • 3
  • 2
6 Comments
 
LVL 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 166 total points
Comment Utility
Main code copied from:
http://volder-notes.blogspot.com/2007/10/removing-duplicate-elements-from-string.html

Not sure how efficient it is over the good old str2tbl and listagg to put it all back together but you can test both and see.

Try this:
WITH t AS (SELECT 'XP78380;XT42564;ZX03942;XP78380;XT42564;' str FROM dual)
    --
select str, rtrim(str_new,',') new_str from t
model
dimension by (0 dim)
measures(str, str||',' str_new)
rules iterate(100) until (str_new[0] = previous(str_new[0]))
(str_new[0]=regexp_replace(str_new[0],'(^|;)([^;]+;)(.*?;)?\2+','\1\2\3'));

Open in new window

0
 

Author Comment

by:Rao_S
Comment Utility
wow! thank you, i need to spend some time and understand how this works, my string is not a constant, at one time it can have 10 elements in it separated by a ';' at another time it might have just 2 elements in it, regardless of the number of elements in it, i need to parse it and remove dups...
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 334 total points
Comment Utility
Assuming the output should be sorted alphabetically

select listagg(str,';') within group (order by str) ||';' from
(SELECT     DISTINCT REGEXP_SUBSTR('XP78380;XT42564;ZX03942;XP78380;XT42564;', '[^;]+', 1, LEVEL) str
      FROM DUAL
CONNECT BY LEVEL <= REGEXP_COUNT('XP78380;XT42564;ZX03942;XP78380;XT42564;', '[^;]+'));


If they won't be sorted alphabetically but you need to preserve the initial order  then


SELECT LISTAGG(str, ';') WITHIN GROUP (ORDER BY lvl) || ';'
  FROM (SELECT str, lvl, ROW_NUMBER() OVER (PARTITION BY str ORDER BY lvl) rn
          FROM (SELECT     REGEXP_SUBSTR('XP78380;XT42564;ZX03942;XP78380;XT42564;', '[^;]+', 1, LEVEL)
                               str,
                           LEVEL lvl
                      FROM DUAL
                CONNECT BY LEVEL <=
                               REGEXP_COUNT('XP78380;XT42564;ZX03942;XP78380;XT42564;', '[^;]+')))
 WHERE rn = 1;
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 334 total points
Comment Utility
and another version,  no aggregation or sorting needed, simply find the dups and remove them


SELECT CASE
           WHEN INSTR(str, 'XT42564', 1, 2) > 0
           THEN
               REGEXP_REPLACE(str, 'XT42564' || ';', NULL, INSTR(str, 'XT42564', 1, 2), 0)
           ELSE
               str
       END
           str
  FROM (SELECT CASE
                   WHEN INSTR(str, 'XP78380', 1, 2) > 0
                   THEN
                       REGEXP_REPLACE(str, 'XP78380' || ';', NULL, INSTR(str, 'XP78380', 1, 2), 0)
                   ELSE
                       str
               END
                   str
          FROM (SELECT 'XP78380;XT42564;ZX03942;XP78380;XT42564;' str FROM DUAL))
0
 

Author Comment

by:Rao_S
Comment Utility
wow!!!!!!
this is wonderful too! i knew regexp was very powerful, but just dint know how to use it!
thank you very much, i love the first select!! also i have never used a connect by level before..
0
 

Author Closing Comment

by:Rao_S
Comment Utility
wonderful solutions!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now