Regex 从oracle中的字符串派生费用

Regex 从oracle中的字符串派生费用,regex,oracle11g,Regex,Oracle11g,我的表格中有一个名为“叙述”的列,其数据如下所示 NARRATION MMT/PS/12345/Family/RAM/chgRs5.00GSTRs0.90 MMT/43425/chgRs5.00GSTRs0.90/1257874554 MMT/XX/12345/Family/RAM/chgRs25.00GSTRs20.90 ITI/4425/chgRs15.00GSTRs10.70/1257874554 从旁白字段中,我想导出费用和GST值,所需输出如下所示 DERIVED NARRATIO

我的表格中有一个名为“叙述”的列,其数据如下所示

NARRATION

MMT/PS/12345/Family/RAM/chgRs5.00GSTRs0.90
MMT/43425/chgRs5.00GSTRs0.90/1257874554
MMT/XX/12345/Family/RAM/chgRs25.00GSTRs20.90
ITI/4425/chgRs15.00GSTRs10.70/1257874554
从旁白字段中,我想导出费用和GST值,所需输出如下所示

DERIVED NARRATION|Charges|GST

chgRs5.00GSTRs0.90|5.00|0.90
chgRs5.00GSTRs0.90|5.00|0.90
chgRs25.00GSTRs20.90|25.00|20.90
chgRs15.00GSTRs10.70|5.00|10.70
SELECT REGEXP_SUBSTR(REGEXP_SUBSTR('MMT/43425/chgRs5.00GSTRs0.90/1257874554','[^/]+',1,3),'[^GSTRs]+',1,2) ,
   REGEXP_SUBSTR(REGEXP_SUBSTR('MMT/43425/chgRs5.00GSTRs0.90/1257874554','[^/]+',1,2+1),'[^GSTRs]+',1,3)
  FROM  DUAL;
请提出疑问。我试过如下所示

DERIVED NARRATION|Charges|GST

chgRs5.00GSTRs0.90|5.00|0.90
chgRs5.00GSTRs0.90|5.00|0.90
chgRs25.00GSTRs20.90|25.00|20.90
chgRs15.00GSTRs10.70|5.00|10.70
SELECT REGEXP_SUBSTR(REGEXP_SUBSTR('MMT/43425/chgRs5.00GSTRs0.90/1257874554','[^/]+',1,3),'[^GSTRs]+',1,2) ,
   REGEXP_SUBSTR(REGEXP_SUBSTR('MMT/43425/chgRs5.00GSTRs0.90/1257874554','[^/]+',1,2+1),'[^GSTRs]+',1,3)
  FROM  DUAL;

但它并没有为所有场景提供正确的输出。

这可能是获得所需内容的一种方法:

select regexp_substr(narration, 'chgRs[^\/]+') as derived_narration,
       regexp_substr(narration, '(chgRs)(.+)(GSTR)', 1, 1, '', 2) as charges,
       regexp_substr(narration, '(GSTRs)(\d+\.\d+)', 1, 1, '', 2) as GST
from yourTable
使用您的示例数据:

SQL> with yourTable(NARRATION) as (
  2      select 'MMT/PS/12345/Family/RAM/chgRs5.00GSTRs0.90' from dual union all
  3      select 'MMT/43425/chgRs5.00GSTRs0.90/1257874554' from dual union all
  4      select 'MMT/XX/12345/Family/RAM/chgRs25.00GSTRs20.90' from dual union all
  5      select 'ITI/4425/chgRs15.00GSTRs10.70/1257874554' from dual
  6  )
  7  select regexp_substr(narration, 'chgRs[^\/]+') as derived_narration,
  8         regexp_substr(narration, '(chgRs)(.+)(GSTR)', 1, 1, '', 2) as charges,
  9         regexp_substr(narration, '(GSTRs)(\d+\.\d+)', 1, 1, '', 2) as GST
 10  from yourTable;

DERIVED_NARRATION         CHARGES    GST
------------------------- ---------- ----------
chgRs5.00GSTRs0.90        5.00       0.90
chgRs5.00GSTRs0.90        5.00       0.90
chgRs25.00GSTRs20.90      25.00      20.90
chgRs15.00GSTRs10.70      15.00      10.70

您不需要正则表达式:

Oracle 11g R2架构设置

CREATE TABLE table_name ( NARRATION ) AS
SELECT 'MMT/PS/12345/Family/RAM/chgRs5.00GSTRs0.90' FROM DUAL UNION ALL
SELECT 'MMT/43425/chgRs5.00GSTRs0.90/1257874554' FROM DUAL UNION ALL
SELECT 'MMT/XX/12345/Family/RAM/chgRs25.00GSTRs20.90' FROM DUAL UNION ALL
SELECT 'ITI/4425/chgRs15.00GSTRs10.70/1257874554' FROM DUAL;
SELECT CASE FIN
       WHEN 0
       THEN SUBSTR( narration, chgrs )
       ELSE SUBSTR( narration, chgrs, fin - chgrs )
       END AS derived_narration,
       SUBSTR( narration, chgrs + 5, gstrs - chgrs - 5 ) AS charges,
       CASE FIN
       WHEN 0
       THEN SUBSTR( narration, gstrs + 5 )
       ELSE SUBSTR( narration, gstrs + 5, fin - gstrs - 5 )
       END AS GST
FROM   (
  SELECT narration,
         INSTR( narration, 'chgRs' ) AS chgrs,
         INSTR( narration, 'GSTRs' ) AS gstrs,
         INSTR( narration, '/', INSTR( narration, 'GSTRs' ) ) AS fin
  FROM   table_name
)
|    DERIVED_NARRATION | CHARGES |   GST |
|----------------------|---------|-------|
|   chgRs5.00GSTRs0.90 |    5.00 |  0.90 |
|   chgRs5.00GSTRs0.90 |    5.00 |  0.90 |
| chgRs25.00GSTRs20.90 |   25.00 | 20.90 |
| chgRs15.00GSTRs10.70 |   15.00 | 10.70 |
|     DERIVED_NARATION | CHARGES |   GST |
|----------------------|---------|-------|
|   chgRs5.00GSTRs0.90 |    5.00 |  0.90 |
|   chgRs5.00GSTRs0.90 |    5.00 |  0.90 |
| chgRs25.00GSTRs20.90 |   25.00 | 20.90 |
| chgRs15.00GSTRs10.70 |   15.00 | 10.70 |
查询1

CREATE TABLE table_name ( NARRATION ) AS
SELECT 'MMT/PS/12345/Family/RAM/chgRs5.00GSTRs0.90' FROM DUAL UNION ALL
SELECT 'MMT/43425/chgRs5.00GSTRs0.90/1257874554' FROM DUAL UNION ALL
SELECT 'MMT/XX/12345/Family/RAM/chgRs25.00GSTRs20.90' FROM DUAL UNION ALL
SELECT 'ITI/4425/chgRs15.00GSTRs10.70/1257874554' FROM DUAL;
SELECT CASE FIN
       WHEN 0
       THEN SUBSTR( narration, chgrs )
       ELSE SUBSTR( narration, chgrs, fin - chgrs )
       END AS derived_narration,
       SUBSTR( narration, chgrs + 5, gstrs - chgrs - 5 ) AS charges,
       CASE FIN
       WHEN 0
       THEN SUBSTR( narration, gstrs + 5 )
       ELSE SUBSTR( narration, gstrs + 5, fin - gstrs - 5 )
       END AS GST
FROM   (
  SELECT narration,
         INSTR( narration, 'chgRs' ) AS chgrs,
         INSTR( narration, 'GSTRs' ) AS gstrs,
         INSTR( narration, '/', INSTR( narration, 'GSTRs' ) ) AS fin
  FROM   table_name
)
|    DERIVED_NARRATION | CHARGES |   GST |
|----------------------|---------|-------|
|   chgRs5.00GSTRs0.90 |    5.00 |  0.90 |
|   chgRs5.00GSTRs0.90 |    5.00 |  0.90 |
| chgRs25.00GSTRs20.90 |   25.00 | 20.90 |
| chgRs15.00GSTRs10.70 |   15.00 | 10.70 |
|     DERIVED_NARATION | CHARGES |   GST |
|----------------------|---------|-------|
|   chgRs5.00GSTRs0.90 |    5.00 |  0.90 |
|   chgRs5.00GSTRs0.90 |    5.00 |  0.90 |
| chgRs25.00GSTRs20.90 |   25.00 | 20.90 |
| chgRs15.00GSTRs10.70 |   15.00 | 10.70 |

CREATE TABLE table_name ( NARRATION ) AS
SELECT 'MMT/PS/12345/Family/RAM/chgRs5.00GSTRs0.90' FROM DUAL UNION ALL
SELECT 'MMT/43425/chgRs5.00GSTRs0.90/1257874554' FROM DUAL UNION ALL
SELECT 'MMT/XX/12345/Family/RAM/chgRs25.00GSTRs20.90' FROM DUAL UNION ALL
SELECT 'ITI/4425/chgRs15.00GSTRs10.70/1257874554' FROM DUAL;
SELECT CASE FIN
       WHEN 0
       THEN SUBSTR( narration, chgrs )
       ELSE SUBSTR( narration, chgrs, fin - chgrs )
       END AS derived_narration,
       SUBSTR( narration, chgrs + 5, gstrs - chgrs - 5 ) AS charges,
       CASE FIN
       WHEN 0
       THEN SUBSTR( narration, gstrs + 5 )
       ELSE SUBSTR( narration, gstrs + 5, fin - gstrs - 5 )
       END AS GST
FROM   (
  SELECT narration,
         INSTR( narration, 'chgRs' ) AS chgrs,
         INSTR( narration, 'GSTRs' ) AS gstrs,
         INSTR( narration, '/', INSTR( narration, 'GSTRs' ) ) AS fin
  FROM   table_name
)
|    DERIVED_NARRATION | CHARGES |   GST |
|----------------------|---------|-------|
|   chgRs5.00GSTRs0.90 |    5.00 |  0.90 |
|   chgRs5.00GSTRs0.90 |    5.00 |  0.90 |
| chgRs25.00GSTRs20.90 |   25.00 | 20.90 |
| chgRs15.00GSTRs10.70 |   15.00 | 10.70 |
|     DERIVED_NARATION | CHARGES |   GST |
|----------------------|---------|-------|
|   chgRs5.00GSTRs0.90 |    5.00 |  0.90 |
|   chgRs5.00GSTRs0.90 |    5.00 |  0.90 |
| chgRs25.00GSTRs20.90 |   25.00 | 20.90 |
| chgRs15.00GSTRs10.70 |   15.00 | 10.70 |
查询2:但是,如果确实要使用正则表达式,则可以使用:

/(chgRs(\d+\.\d+)GSTRs(\d+\.\d+))(/|$)
像这样:

SELECT REGEXP_SUBSTR( narration, '/(chgRs(\d+\.\d+)GSTRs(\d+\.\d+))(/|$)', 1, 1, NULL, 1 )
         AS derived_naration,
       REGEXP_SUBSTR( narration, '/(chgRs(\d+\.\d+)GSTRs(\d+\.\d+))(/|$)', 1, 1, NULL, 2 )
         AS charges,
       REGEXP_SUBSTR( narration, '/(chgRs(\d+\.\d+)GSTRs(\d+\.\d+))(/|$)', 1, 1, NULL, 3 )
         AS gst
FROM   table_name

CREATE TABLE table_name ( NARRATION ) AS
SELECT 'MMT/PS/12345/Family/RAM/chgRs5.00GSTRs0.90' FROM DUAL UNION ALL
SELECT 'MMT/43425/chgRs5.00GSTRs0.90/1257874554' FROM DUAL UNION ALL
SELECT 'MMT/XX/12345/Family/RAM/chgRs25.00GSTRs20.90' FROM DUAL UNION ALL
SELECT 'ITI/4425/chgRs15.00GSTRs10.70/1257874554' FROM DUAL;
SELECT CASE FIN
       WHEN 0
       THEN SUBSTR( narration, chgrs )
       ELSE SUBSTR( narration, chgrs, fin - chgrs )
       END AS derived_narration,
       SUBSTR( narration, chgrs + 5, gstrs - chgrs - 5 ) AS charges,
       CASE FIN
       WHEN 0
       THEN SUBSTR( narration, gstrs + 5 )
       ELSE SUBSTR( narration, gstrs + 5, fin - gstrs - 5 )
       END AS GST
FROM   (
  SELECT narration,
         INSTR( narration, 'chgRs' ) AS chgrs,
         INSTR( narration, 'GSTRs' ) AS gstrs,
         INSTR( narration, '/', INSTR( narration, 'GSTRs' ) ) AS fin
  FROM   table_name
)
|    DERIVED_NARRATION | CHARGES |   GST |
|----------------------|---------|-------|
|   chgRs5.00GSTRs0.90 |    5.00 |  0.90 |
|   chgRs5.00GSTRs0.90 |    5.00 |  0.90 |
| chgRs25.00GSTRs20.90 |   25.00 | 20.90 |
| chgRs15.00GSTRs10.70 |   15.00 | 10.70 |
|     DERIVED_NARATION | CHARGES |   GST |
|----------------------|---------|-------|
|   chgRs5.00GSTRs0.90 |    5.00 |  0.90 |
|   chgRs5.00GSTRs0.90 |    5.00 |  0.90 |
| chgRs25.00GSTRs20.90 |   25.00 | 20.90 |
| chgRs15.00GSTRs10.70 |   15.00 | 10.70 |

你得到了什么输出?