oracle查找并添加前缀和后缀到varchar2列中的子字符串

时间:2018-05-28 08:22:27

标签: regex oracle replace

我们想在varchar2列中查找并添加不同的前缀和后缀。我提供了示例字符串。我想查找并添加前缀和后缀到substring

"<ac:structured-macro ac:name="toc">this is atable of content macro</ac:structured-macro>"

并将其更改为

<ac:structured-macro ac:name="scroll-ignore" ><ac:structured-macro ac:name="toc">this is atable of content macro</ac:structured-macro></ac:structured-macro>

示例列字符串: “{TOC:轮廓=真|可印刷= FALSE}

<ac:structured-macro ac:name="toc">this is a table of content macro</ac:structured-macro>

结果观察允许临床医生在患者环境中检查实验室,放射学和生命体征结果。这使得临床医生可以为患者提供最新的结果。“

我尝试使用正则表达式替换,但无法在单个查询中执行此操作

update bodycontent set body=REGEXP_REPLACE(body, '(<ac:structured-macro ac:name="toc">)', '<ac:structured-macro ac:name="scroll-ignore"> <ac:structured-macro ac:name="toc">') where contentid=716785

update bodycontent set body=REGEXP_REPLACE(body, '(<\/ac:structured-macro>)', '</ac:structured-macro></ac:structured-macro>',1,1) where contentid=716785

1 个答案:

答案 0 :(得分:0)

如果您简化示例,您的问题会更容易理解:

CREATE TABLE bodycontent (body VARCHAR2(2000 CHAR));
INSERT INTO bodycontent VALUES ('<foo>text</foo>');
SELECT * FROM bodycontent;
<foo>text</foo> 

UPDATE bodycontent SET body = REGEXP_REPLACE(body, 
    '(<foo>.*</foo>)', 
    '<bar>\1</bar>');

SELECT * FROM bodycontent;
<bar><foo>text</foo></bar>

可以吗?如果是这样,您的真实数据将如下所示:

INSERT INTO bodycontent VALUES (
   '<ac:structured-macro ac:name="toc">'||
   'this is atable of content macro</ac:structured-macro>');


UPDATE bodycontent SET body = REGEXP_REPLACE(body, 
    '(<ac:structured-macro ac:name="toc">.*</ac:structured-macro>)', 
     '<ac:structured-macro ac:name="scroll-ignore">\1</ac:structured-macro>');

SELECT * FROM bodycontent;
<ac:structured-macro ac:name="scroll-ignore">
   <ac:structured-macro ac:name="toc">
     this is atable of content macro
   </ac:structured-macro>
</ac:structured-macro>