首页 > 解决方案 > 无法使用 oracle 正则表达式在带有管道的复合分隔符上拆分

问题描述

我正在尝试拆分具有两个复合和嵌套分隔符的字符串:

问题是正则表达式模式适用于单个分隔符,例如: ~or |or 它也适用于复合分隔符,例如~~or ^^,但它不适用于上述分隔符。

目标字符串:

*~~36415^^Description^^Version-4~~70450^^Description2^^Version-4~~73110^^Description3^^Version-4~~73140*

使用的正则表达式:* [^(~|)]* * [^(~\|)]* * ((?!((~)(\|))).) *(?!(~\|).)

但它没有用。但是,当我将目标字符串更改为:

36415^^MRI Orbit, Face, Neck W W/O Contrast^^CPT-4~~70450^^MRI Orbit, Face, Neck W W/O Contrast^^CPT-4~~73110^^MRI Orbit, Face, Neck W W/O Contrast^^CPT-4~~73140

并使用正则表达式: * [^(~~)]* *[^(^^)]*

有用。

PS:我使用https://regex101.com/r/Stbwxt/1对此进行测试。

WITH String_splits AS (
        SELECT TRIM(',' FROM REGEXP_SUBSTR('~|36415^|Description^|Version-4~|70450^|Description2^|Version-4~|73110^|Description3^|Version-4~|73140', '[^(~|)]*', 1, LEVEL)) String_splits_1
        , TRIM(',' FROM REGEXP_SUBSTR('~|36415^|Description^|Version-4~|70450^|Description2^|Version-4~|73110^|Description3^|Version-4~|73140', '[^(~|)]*', 3, LEVEL)) String_splits_2
        FROM dual
        CONNECT BY LEVEL <= REGEXP_COUNT('~|36415^|Description^|Version-4~|70450^|Description2^|Version-4~|73110^|Description3^|Version-4~|73140', '[^(~|)]*')
)
SELECT String_splits_1
        , String_splits_2
        FROM String_splits;

标签: regexoracleoracle11g

解决方案


我不确定初始输入字符串是什么;我希望这一个。这样的事情有意义吗?这个想法是:用不同的东西(例如分号)替换当前分隔符,然后将字符串拆分为行。

SQL> with test (col) as
  2    (select '~|36415^|Description^|Version-4~|70450^|Description2^|Version-4~|73110^|Description3^|Version-4~|73140'
  3     from dual
  4    ),
  5  t_replaced as
  6    (select replace(replace(col, '~|', ';'), '^|', ';') rep
  7     from test
  8    )
  9  select regexp_substr(rep, '[^;]+', 1, level) result
 10  from t_replaced
 11  connect by level <= regexp_count(rep, ';') + 1;

RESULT
--------------------------------------------------------------------------------
36415
Description
Version-4
70450
Description2
Version-4
73110
Description3
Version-4
73140


11 rows selected.

SQL>

推荐阅读