Oracle SQL:正则表达式 - 从行特定字符开始

时间:2017-04-12 11:56:58

标签: oracle

  1. 我有这个列RULE有超过300行(如下所示),我需要在select语句中使用一个正则表达式,以便检索(在不同的列中)以下内容:
  2. MisTop50RankingTop50CCP& MisScLeDivProfit为第X列
  3. TUKB901作为第Y列
  4. PA_T50_TOP50& PA_SC_LE_DIV为Z列

    <regel><wenn wert="MisTop50RankingTop50CCP" /><dann wert="T50" /><dann wert="TUKB901" /><dann wert="PA_T50_TOP50" /><dann wert="TUKB203" /><dann wert="T_T50_TOP50" /><dann wert="STICHTAG" /><dann wert="CCP" /><dann wert=" " /><dann wert=" " /><dann wert="1" /><dann wert=" " /><dann wert=" " /><prio wert="50"/></regel>
    <regel><wenn wert="MisScLeDivProfit" /><dann wert="SC" /><dann wert="TUKB901" /><dann wert="PA_SC_LE_DIV" /><dann wert="TUKB201" /><dann wert="T_SC_LE_DIV" /><dann wert="STICHTAG_MANDANT_LAUF" /><dann wert=" " /><dann wert=" " /><dann wert=" " /><dann wert="1" /><dann wert=" " /><dann wert=" " /><prio wert="50"/></regel>
    
  5. 任何想法都会有很大帮助

    谢谢

2 个答案:

答案 0 :(得分:0)

为什么要使用正则表达式?您也可以使用XML函数执行此操作。假设每一行都返回一条记录,你可以使用这一条:

WITH x AS (
    SELECT XMLTYPE('<regel><wenn wert="MisTop50RankingTop50CCP" /><dann wert="T50" /><dann wert="TUKB901" /><dann wert="PA_T50_TOP50" /><dann wert="TUKB203" /><dann wert="T_T50_TOP50" /><dann wert="STICHTAG" /><dann wert="CCP" /><dann wert=" " /><dann wert=" " /><dann wert="1" /><dann wert=" " /><dann wert=" " /><prio wert="50"/></regel>') AS regel
FROM dual)
SELECT WENN AS X,
    DANN_1 AS Y,
    DANN_3 AS Z
FROM x
    NATURAL JOIN XMLTABLE('/regel' PASSING regel COLUMNS
    WENN VARCHAR2(100) PATH 'wenn/@wert',
    DANN_1 VARCHAR2(100) PATH 'dann[1]/@wert',
    DANN_3 VARCHAR2(100) PATH 'dann[3]/@wert'
); 


X                       Y   Z
MisTop50RankingTop50CCP T50 PA_T50_TOP50

在你的情况下,它可能看起来像这样:

SELECT WENN AS X,
    DANN_1 AS Y,
    DANN_3 AS Z
FROM YOUR_TABLE
    NATURAL JOIN XMLTABLE('/regel' PASSING XMLTYPE(RULE) COLUMNS
    WENN VARCHAR2(100) PATH 'wenn/@wert',
    DANN_1 VARCHAR2(100) PATH 'dann[1]/@wert',
    DANN_3 VARCHAR2(100) PATH 'dann[3]/@wert'
); 

答案 1 :(得分:0)

  

我需要使用REGEXP_SUBSTR,因为它将成为更大选择的一部分,这是客户想要的

真的,do not use regular expressions to parse XML并使用正确的XML解析器。

如果你被要求反对所有良好的意识:

SELECT REGEXP_SUBSTR( rule, '<wenn\s+wert\s*=\s*"(.*?)"\s*/>', 1, 1, NULL, 1 ) AS X,
       REGEXP_SUBSTR( rule, '<dann\s+wert\s*=\s*"(.*?)"\s*/>', 1, 2, NULL, 1 ) AS Y,
       REGEXP_SUBSTR( rule, '<dann\s+wert\s*=\s*"(.*?)"\s*/>', 1, 3, NULL, 1 ) AS Z
FROM   your_table;