Link to home
Start Free TrialLog in
Avatar of skahlert2010
skahlert2010

asked on

REGEXP_REPLACE Oracle

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
ASKER CERTIFIED SOLUTION
Avatar of Sean Stuber
Sean Stuber

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sean Stuber
Sean Stuber

if you're on 11g you can use subexpressions

regexp_substr(group_name, 'CN=([^,]+)',1,1,null,1)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of skahlert2010

ASKER

Ingenius!

Thanks sdstuber! It's working perfectly!

How did you figure out the correct syntax?

Regards,

Skahlert2010
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
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!