我想从VAL列中提取一些字符串,根据下面的正则表达式以粗体显示。这是我在源代码中的数据示例:
Table1
-----------------
ID VAL
-----------------
1 GR-RDE
2 GR-RZA-RDE
3 GR-RZA-RDE_RZA
4 GR-RGS
5 GR-RZA-OR-ORC
6 GR-RZA-RDE-OR-ORC_RZA
期望的结果:
> Output
-----------------
ID RESULT
-----------------
1 RDE
2 RZA
2 RDE
3 RZA
3 RDE
4 RGS
5 RZA
5 OR
6 RZA
6 RDE
6 OR
要做到这一点,我已经完成了这个正则表达式: 的(小于?= - )(?![A-Z])(RDE | RZA | | RGS OR)
我的问题是我找不到在SQL语句(Oracle数据库)中使用此正则表达式的方法。我试图用这样的东西进行测试,返回Null:
select REGEXP_SUBSTR(VAL,'(?<=-)(RDE|RZA|RGS|OR)(?![A-Z])') from Table1;
答案 0 :(得分:1)
Oracle 11g R2架构设置:
CREATE TABLE Table1 ( ID, VAL ) AS
SELECT 1, 'GR-RDE' FROM DUAL UNION ALL
SELECT 2, 'GR-RZA-RDE' FROM DUAL UNION ALL
SELECT 3, 'GR-RZA-RDE_RZA' FROM DUAL UNION ALL
SELECT 4, 'GR-RGS' FROM DUAL UNION ALL
SELECT 5, 'GR-RZA-OR-ORC' FROM DUAL UNION ALL
SELECT 6, 'GR-RZA-RDE-OR-ORC_RZA' FROM DUAL
查询1 :
WITH words ( id, val, lvl, str, maxlvl ) AS (
SELECT id,
val,
1,
REGEXP_SUBSTR( val, '[A-Z]+', 1, 1 ),
REGEXP_COUNT( val, '[A-Z]+' )
FROM table1
UNION ALL
SELECT id,
val,
lvl + 1,
REGEXP_SUBSTR( val, '[A-Z]+', 1, lvl + 1 ),
maxlvl
FROM words
WHERE lvl < maxlvl
)
SELECT id, str, lvl
FROM words
ORDER BY id, lvl
<强> Results 强>:
| ID | STR | LVL |
|----|-----|-----|
| 1 | GR | 1 |
| 1 | RDE | 2 |
| 2 | GR | 1 |
| 2 | RZA | 2 |
| 2 | RDE | 3 |
| 3 | GR | 1 |
| 3 | RZA | 2 |
| 3 | RDE | 3 |
| 3 | RZA | 4 |
| 4 | GR | 1 |
| 4 | RGS | 2 |
| 5 | GR | 1 |
| 5 | RZA | 2 |
| 5 | OR | 3 |
| 5 | ORC | 4 |
| 6 | GR | 1 |
| 6 | RZA | 2 |
| 6 | RDE | 3 |
| 6 | OR | 4 |
| 6 | ORC | 5 |
| 6 | RZA | 6 |
查询2 :
SELECT t.id, w.COLUMN_VALUE AS str
FROM Table1 t
CROSS JOIN
TABLE(
CAST(
MULTISET(
SELECT REGEXP_SUBSTR( t.val, '[A-Z]+', 1, LEVEL )
FROM DUAL
CONNECT BY LEVEL <= REGEXP_COUNT( t.val, '[A-Z]+' )
) AS SYS.ODCIVARCHAR2LIST
)
) w
<强> Results 强>:
| ID | STR |
|----|-----|
| 1 | GR |
| 1 | RDE |
| 2 | GR |
| 2 | RZA |
| 2 | RDE |
| 3 | GR |
| 3 | RZA |
| 3 | RDE |
| 3 | RZA |
| 4 | GR |
| 4 | RGS |
| 5 | GR |
| 5 | RZA |
| 5 | OR |
| 5 | ORC |
| 6 | GR |
| 6 | RZA |
| 6 | RDE |
| 6 | OR |
| 6 | ORC |
| 6 | RZA |