Solved

REGEXP_REPLACE Oracle

Posted on 2011-03-11
6
1,118 Views
Last Modified: 2012-06-27
Dear experts!

I need to extract "lwf-gr-gg-abt1" from the following string in sql:

CN=lwf-gr-gg-abt1,OU=abt_neu,OU=gruppen,OU=LWF,DC=my,DC=local,DC=domain

I want to use a regular expression such and tried the following without success:

select user_name, regexp_replace(group_name, 'CN=(.*),CN=(.*)','\1') from table

Do you have a regualr expression that will work for me?

Many thanks for your help and thoughts,

skahlert2010
0
Comment
Question by:skahlert2010
  • 3
  • 2
6 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 400 total points
ID: 35108186
substr(regexp_substr(group_name, 'CN=[^,]+'),4)
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 35108219
if you're on 11g you can use subexpressions

regexp_substr(group_name, 'CN=([^,]+)',1,1,null,1)
0
 
LVL 7

Assisted Solution

by:Gene_Cyp
Gene_Cyp earned 100 total points
ID: 35108250
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.

 

Author Comment

by:skahlert2010
ID: 35108268
Ingenius!

Thanks sdstuber! It's working perfectly!

How did you figure out the correct syntax?

Regards,

Skahlert2010
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 35108316
how?

it's all about patterns then figuring out what rule defines that pattern.


So, first, how do I identify the string you want.
I figured the pattern was everything between "CN="  and ","

so,  the expression was CN=  at the start, that part was easy.
continuing until the comma meant I had to take everything that wasn't a comma

[^,]  means not-comma

[^,]+ means 1 or more not-comma

then it's just a matter of pulling the "CN="  part off  and in 10g use substr to skip the first 3
in 11g, wrap it in parentheses and choose that subexpression
0
 

Author Comment

by:skahlert2010
ID: 35108373
Thanks for clarifying your attempt!

Makes it much easier to understand!

Thank you Gene_Cyp for pointing at the oracle resource! I have it already bookmarked!

Have a great weekend guys!
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

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and theā€¦
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

747 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

12 Experts available now in Live!

Get 1:1 Help Now