regex - 无法使用 oracle 正则表达式在带有管道的复合分隔符上拆分
问题描述
我正在尝试拆分具有两个复合和嵌套分隔符的字符串:
- 1级:
~|
- 2级:
^|
问题是正则表达式模式适用于单个分隔符,例如:
~
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;
解决方案
我不确定初始输入字符串是什么;我希望这是一个。这样的事情有意义吗?这个想法是:用不同的东西(例如分号)替换当前分隔符,然后将字符串拆分为行。
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>
推荐阅读
- spring - 为什么@ConditionalOnEnabledEndpoint 注释需要@Configuration 类?
- flutter - Flutter:如何在 Firestore 中获取数据并在下拉按钮中显示这些数据?
- c# - 如何使用 C# 进行双面打印?
- java - 图像滑块:如何使用 JavaFX 将图像放在另一个图像上
- node.js - Nodejs Express 重定向到可共享的唯一 url,客户端 JS 工作
- python - 如何从元组列表中获取第二项,然后将其与 python shell 中的给定数字进行比较
- c++ - Chrome扩展和win32 c++应用程序之间的两种方式通信
- python - python和c ++之间的张量流图传输?
- reactjs - React 组件:声明或声明预期 ts(1128)
- codeblocks - 如何更改 Code::Blocks 中注释的颜色?