REGEXP_REPLACE替换字符串时出错

时间:2019-05-08 04:34:54

标签: sql regex oracle

我希望将以下字符串转换为所需的格式。我将具有以下几个值。有一个简单的方法可以做到这一点。我尝试使用REGEXP_REPLACE,但无法正常工作:

列数据

Region[Coding Analyst|||BA|||reg pro|||04561|||08/16/2011|||Board member|||AZ|||06340|||Whiter Bridge|||CA|||M0673|||West Region 09|||K04956|||East Division|||Supreme]

必填数据

{actingname=06340, actingid=M0673, insturmentid=BA, insturmentname=Coding Analyst, commonname=West Region 09, stdate=08/16/2011, linnumber=04561, linstate=CA, linname=Supreme}

问题在于获取字符串的10、11、12和15位置。我可以得到低于10位的任何东西,但不能达到10位或更多的字符串位置。你能指导我在这里我想念的吗

 SELECT REGEXP_REPLACE(Region[Coding Analyst|||BA|||reg pro|||04561|||08/16/2011|||Board member|||AZ|||06340|||Whiter Bridge|||CA|||M0673|||West Region 09|||K04956|||East Division|||Supreme],'^Region\[([[:alpha:][:space:][:digit:]]*)\|\|\|([[:alpha:]]*)\|\|\|([[:alpha:][:space:][:punct:]]*)\|\|\|([[:digit:][:alpha:]]*)\|\|\|([[:digit:][:punct:]]*)\|\|\|([[:alpha:][:space:]]*)\|\|\|([[:alpha:]]*)\|\|\|([[:digit:]]*)\|\|\|([[:alpha:][:space:]]*)\|\|\|([[:alpha:]]*)\|\|\|([[:digit:][:alpha:]]*)\|\|\|([[:digit:][:alpha:][:space:]]*)\|\|\|([[:digit:][:alpha:]]*)\|\|\|([[:alpha:][:space:]]*)\|\|\|([[:alpha:]]*).*','{actingname=\8,actingid=\11,insturmentid=\2,insturmentname=\1,commonname=\12, stdate=\5,linnumber=4,linstate=10,linname=15}']') as replaced
    FROM dual;--Here 10,11,12 and 15 position are not being fetched

根据@ MT0反馈的修订查询,因为我有一些行以null和文本“ null”作为值。我收到错误ORA-00932:数据类型不一致:预期的CHAR得到了CLOB

 SELECT
            '{'
       ||   'actingname='     || REGEXP_SUBSTR( value, '([a-zA-Z0-9 ]*)(\|\|\||\])', 1,  8, NULL, 1 )
       || ', actingid='       || REGEXP_SUBSTR( value, '([a-zA-Z0-9 ]*)(\|\|\||\])', 1, 11, NULL, 1 )
       || ', insturmentid='   || REGEXP_SUBSTR( value, '([a-zA-Z0-9 ]*)(\|\|\||\])', 1,  2, NULL, 1 )
       || ', insturmentname=' || REGEXP_SUBSTR( value, '([a-zA-Z0-9 ]*)(\|\|\||\])', 1,  1, NULL, 1 )
       || ', commonname='     || REGEXP_SUBSTR( value, '([a-zA-Z0-9 ]*)(\|\|\||\])', 1, 12, NULL, 1 )
       || ', stdate='         || REGEXP_SUBSTR( value, '([a-zA-Z0-9 ]*)(\|\|\||\])', 1,  5, NULL, 1 )
       || ', linnumber='      || REGEXP_SUBSTR( value, '([a-zA-Z0-9 ]*)(\|\|\||\])', 1,  4, NULL, 1 )
       || ', linstate='       || REGEXP_SUBSTR( value, '([a-zA-Z0-9 ]*)(\|\|\||\])', 1, 10, NULL, 1 )
       || ', linname='        || REGEXP_SUBSTR( value, '([a-zA-Z0-9 ]*)(\|\|\||\])', 1, 15, NULL, 1 )
       || '}'
       AS replaced
    FROM test_data
where value is not null and value <>'null'

1 个答案:

答案 0 :(得分:1)

使用REGEXP_SUBSTR

Oracle设置

CREATE TABLE test_data( value ) AS
  SELECT 'Region[Coding Analyst|||BA|||reg pro|||04561|||08/16/2011|||Board member|||AZ|||06340|||Whiter Bridge|||CA|||M0673|||West Region 09|||K04956|||East Division|||Supreme],'
  FROM dual;

查询

SELECT    '{'
       ||   'actingname='     || REGEXP_SUBSTR( value, '([a-zA-Z0-9 ]*)(\|\|\||\])', 1,  8, NULL, 1 )
       || ', actingid='       || REGEXP_SUBSTR( value, '([a-zA-Z0-9 ]*)(\|\|\||\])', 1, 11, NULL, 1 )
       || ', insturmentid='   || REGEXP_SUBSTR( value, '([a-zA-Z0-9 ]*)(\|\|\||\])', 1,  2, NULL, 1 )
       || ', insturmentname=' || REGEXP_SUBSTR( value, '([a-zA-Z0-9 ]*)(\|\|\||\])', 1,  1, NULL, 1 )
       || ', commonname='     || REGEXP_SUBSTR( value, '([a-zA-Z0-9 ]*)(\|\|\||\])', 1, 12, NULL, 1 )
       || ', stdate='         || REGEXP_SUBSTR( value, '([a-zA-Z0-9 ]*)(\|\|\||\])', 1,  5, NULL, 1 )
       || ', linnumber='      || REGEXP_SUBSTR( value, '([a-zA-Z0-9 ]*)(\|\|\||\])', 1,  4, NULL, 1 )
       || ', linstate='       || REGEXP_SUBSTR( value, '([a-zA-Z0-9 ]*)(\|\|\||\])', 1, 10, NULL, 1 )
       || ', linname='        || REGEXP_SUBSTR( value, '([a-zA-Z0-9 ]*)(\|\|\||\])', 1, 15, NULL, 1 )
       || '}' AS replaced
FROM   test_data

输出

| REPLACED                                                                                                                                                                  |
| :------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| {actingname=06340, actingid=M0673, insturmentid=BA, insturmentname=Coding Analyst, commonname=West Region 09, stdate=2011, linnumber=04561, linstate=CA, linname=Supreme} |

db <>提琴here