如何从查询结果中删除不需要的文本/字符

时间:2015-02-17 22:57:33

标签: sql oracle

我正在寻找从以下查询的结果中排除不需要的文字的最佳方法:

QUERY:

select message_
from jbpm_comment
order by time_ desc

结果:

enter image description here 出于安全原因,我已将网址涂黑了。突出显示的文字 "这是另一项测试。我会接受!" 是我想要归还的。我需要想办法放弃现场其他所有东西:

需要放弃:

  • 在我需要保留的文本之前: 2015测试计划^ | ^^ | ^ g9xg3 ^ | ^ read ^ | ^^ | ^ 2015测试计划 - 人力资源审批已被接受^ | ^
  • 根据我需要保留的文字: html a href =" https:// URL_REMOVED_>点击此处查看详细信息

我还没有能够根据需要整合TRIM,但我可能会错过一些功能性步骤。任何建议表示赞赏。谢谢!

2 个答案:

答案 0 :(得分:1)

根据您描述的模式,您可以使用正则表达式删除您不想要的部分。您似乎有^|^分隔的元素,因此您可以删除由the regexp_replace() function和模式(\^\|\^.*\^\|\^)(转义特殊字符)的对包围的任何内容,但您也想要摆脱第一个分隔符之前的文本;您可以稍微简化一下,但作为起点,使用CTE提供两个样本值:

with t as (
  select '2015 Test Plan^|^^|^g9xg3^|^read^|^^|^2015 Test Plan - HR Approval has been accepted^|^This is another test. I will accept!<html><a href="https://URL_REMOVED_> Click here to view the details' as text from dual
  union all select 'TemplateName^|^^|^USERID^|^read/unread^|^^|^Accepted or Inquired^|^THE TEXT I WANT TO KEEP <html>web url' as text from dual
)
select regexp_replace(text, '^.*(\^\|\^.*\^\|\^)')
from t;

KEEP                                                                                                    
--------------------------------------------------------------------------------------------------------
This is another test. I will accept!<html><a href="https://URL_REMOVED_> Click here to view the details  
THE TEXT I WANT TO KEEP <html>web url                                                                    

然后,结果可以输入第二个regexp_replace,删除以<html>开头的任何内容:

with t as (
  select '2015 Test Plan^|^^|^g9xg3^|^read^|^^|^2015 Test Plan - HR Approval has been accepted^|^This is another test. I will accept!<html><a href="https://URL_REMOVED_> Click here to view the details' as text from dual
  union all select 'TemplateName^|^^|^USERID^|^read/unread^|^^|^Accepted or Inquired^|^THE TEXT I WANT TO KEEP <html>web url' as text from dual
)
select regexp_replace(regexp_replace(text, '^.*(\^\|\^.*\^\|\^)'), '<html.*') as keep
from t;

KEEP                                    
----------------------------------------
This is another test. I will accept!     
THE TEXT I WANT TO KEEP                  

Read more about Oracle's regular expression support

答案 1 :(得分:0)

试试这个。 (我不确定它是否是您要排除的静态文本,但如果是这样,这可能对您有用)

select replace('blabla I will accept blabla','I will accept','') from dual;

现在只有&#39; blabla blabla&#39;左