我正在使用Oracle SQL 11g R2尝试以下字符串搜索:
数据是:
| CN = aXYZApple-Au,OU =托管,OU =组,DC =核心,DC = dir,DC = abc,DC = com | CN = 31107427,OU =分布列表,OU =共享 邮箱,DC =核心,DC = dir,DC = abc,DC = com | CN = ea90045052,OU = Groups,OU = eProfile,DC = core,DC = dir,DC = abc,DC = com | CN = S0901448,OU =分布列表,OU =共享 邮箱,DC =核心,DC = dir,DC = abc,DC = com | CN = 00900887,OU =分发 列表,OU =共享邮箱,DC =核心,DC = dir,DC = abc,DC = com | CN = NSMMMM,OU = LRP,OU =组,DC =核心,DC = dir,DC = abc,DC = com | CN = aXYZApple-只读,OU =管理,OU =组,DC =芯,DC = DIR,DC = ABC,DC = com的 | CN = WWSWW-Au,OU = LRP,OU =组,DC =核心,DC = dir,DC = abc,DC = com | CN = aLogical_RW,OU =托管,OU =组,DC =核心,DC = dir,DC = abc,DC = com | CN = aXYZApple-Write,OU = Managed,OU = Groups,DC = core,DC = dir,DC = abc,DC = com |
从上面的数据中,我需要获取所有包含字符串“aXYZApple”的字符串,只有“OU = Managed”作为此字符串匹配的第二部分。
基于以上所述,以下结果就是我所追求的,并且只会显示:
aXYZApple-Au
aXYZApple-Readonly
aXYZApple-Write
使用的是Oracle regexp_like / regexp_replace。
答案 0 :(得分:7)
您可以使用regexp_substr
将输入数据拆分为行,然后查找相关字符串,例如:
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
这里有一点解释。您必须逐步完成查询。
查询的内部部分将遍历您的数据(对于每个|
):
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 |
然后,您将在第二个位置循环OU=Managed
字符串:
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,
最后,选择最后regexp_substr
的相关部分。