2

I am trying the following string search using Oracle SQL 11g R2:

Data is:

| CN=aXYZApple-Au,OU=Managed,OU=Groups,DC=core,DC=dir,DC=abc,DC=com | CN=31107427,OU=Distribution Lists,OU=Shared Mailboxes,DC=core,DC=dir,DC=abc,DC=com | CN=ea90045052,OU=Groups,OU=eProfile,DC=core,DC=dir,DC=abc,DC=com | CN=S0901448,OU=Distribution Lists,OU=Shared Mailboxes,DC=core,DC=dir,DC=abc,DC=com | CN=00900887,OU=Distribution Lists,OU=Shared Mailboxes,DC=core,DC=dir,DC=abc,DC=com | CN=NSMMMM,OU=LRP,OU=Groups,DC=core,DC=dir,DC=abc,DC=com | CN=aXYZApple-Readonly,OU=Managed,OU=Groups,DC=core,DC=dir,DC=abc,DC=com | CN=WWSWW-Au,OU=LRP,OU=Groups,DC=core,DC=dir,DC=abc,DC=com | CN=aLogical_RW,OU=Managed,OU=Groups,DC=core,DC=dir,DC=abc,DC=com | CN=aXYZApple-Write,OU=Managed,OU=Groups,DC=core,DC=dir,DC=abc,DC=com |

From the above data, I need to obtain all strings that hold the string "aXYZApple" only with the "OU=Managed" as the second part of this string match.

So based on the above, the following results is what I am after and would be displayed only:

aXYZApple-Au
aXYZApple-Readonly
aXYZApple-Write

Am using Oracle regexp_like/regexp_replace.

1 Answer 1

7

You could use regexp_substr to split your input data into lines and then look for the relevant strings, for example:

SQL> SELECT regexp_substr(line, 'aXYZApple[^,]*') subtxt
  2    FROM (SELECT regexp_substr(:x, '[^|]*\|', 1, rownum + 1) line
  3             FROM dual
  4           CONNECT BY LEVEL <= length(:x) - length(REPLACE(:x, '|', '')))
  5   WHERE regexp_substr(line || ',', '[^,]*,', 1, 2) = 'OU=Managed,'
  6     AND line LIKE '%aXYZApple%';

SUBTXT
--------------------------------------------------------------------------------
aXYZApple-Au
aXYZApple-Readonly
aXYZApple-Write

Here's a little explanation. You have to go through the query step by step.

The inner part of the query will loop through your data (for each |):

SQL> SELECT regexp_substr(:x, '[^|]*\|', 1, rownum + 1) line
  2    FROM dual
  3  CONNECT BY LEVEL <= length(:x) - length(REPLACE(:x, '|', ''));

LINE
--------------------------------------------------------------------------------
 CN=aXYZApple-Au,OU=Managed,OU=Groups,DC=core,DC=dir,DC=abc,DC=com |
 CN=31107427,OU=Distribution Lists,OU=Shared Mailboxes,DC=core,DC=dir,DC=abc,DC=
 CN=ea90045052,OU=Groups,OU=eProfile,DC=core,DC=dir,DC=abc,DC=com |
 CN=S0901448,OU=Distribution Lists,OU=Shared Mailboxes,DC=core,DC=dir,DC=abc,DC=
 CN=00900887,OU=Distribution Lists,OU=Shared Mailboxes,DC=core,DC=dir,DC=abc,DC=
 CN=NSMMMM,OU=LRP,OU=Groups,DC=core,DC=dir,DC=abc,DC=com |
 CN=aXYZApple-Readonly,OU=Managed,OU=Groups,DC=core,DC=dir,DC=abc,DC=com |
 CN=WWSWW-Au,OU=LRP,OU=Groups,DC=core,DC=dir,DC=abc,DC=com |
 CN=aLogical_RW,OU=Managed,OU=Groups,DC=core,DC=dir,DC=abc,DC=com |
 CN=aXYZApple-Write,OU=Managed,OU=Groups,DC=core,DC=dir,DC=abc,DC=com |

You would then loop for the OU=Managed string in the second position:

SQL> SELECT regexp_substr(line || ',', '[^,]*,', 1, 2) second_part
  2    FROM (SELECT regexp_substr(:x, '[^|]*\|', 1, rownum + 1) line
  3             FROM dual
  4           CONNECT BY LEVEL <= length(:x) - length(REPLACE(:x, '|', '')));

SECOND_PART
--------------------------------------------------------------------------------
OU=Managed,
OU=Distribution Lists,
OU=Groups,
OU=Distribution Lists,
OU=Distribution Lists,
OU=LRP,
OU=Managed,
OU=LRP,
OU=Managed,
OU=Managed,

Finally, select the relevant part with a last regexp_substr.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for that but just wondering if you could please provide a short description of what your query is doing - would really appreciate it. thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.