如何提取2个字符串之间的字符串

时间:2019-05-14 19:01:15

标签: sql regex oracle

需要帮助从CLOB字段(称为“ note”的字段)中提取字符串

我想在下面显示的字符串中提取“添加的原因”到“删除的原因”之间的所有内容。 为了使其更加复杂,有时并没有出现删除的原因,因此我需要将所有原因都添加到最后。 另外,我不想在括号“()”中使用字符串。 到目前为止,我已经设法从所需字符串中仅提取了一部分。

CLOB值:

"Change step forward
Note added
Step changed from [OFF] to [ON]
Reasons added: test1 (Type), some test 2 (Type), test3 , sometest4& and5(Type)
Reasons removed: test6- test7(Type)"


TO_CHAR(TRIM(
    regexp_substr(
    (case when note like '%Reasons removed:%' then regexp_substr(note, 'Reasons added:\s(.*?)\sReasons removed:.*', 1, 1, NULL, 1) 
    else (case when note like '%Reasons added:%' then REGEXP_SUBSTR(note,'[^:]+$') end) end),'[^(]+'))) "CODE"

test1 , some test 2, test3 , sometest4& and5

2 个答案:

答案 0 :(得分:0)

这个怎么样?我在表中添加了另一个值,该值不包含“已删除原因”字符串。

基本上,它会选择“添加的原因”和“删除的原因”之间的所有内容(如果存在);如果没有,则从“添加原因”到末尾。 CASE会注意这一点。

SQL> select * from test;

COL
----------------------------------------------------------------------------------------------
"Change step forward
Note added
Step changed from [OFF] to [ON]
Reasons added: test1 (Type), some test 2 (Type), test3 , sometest4& and5(Type)
Reasons removed: test6- test7(Type)"

"This is text without "Reas. remov."
Note added
Step changed from [OFF] to [ON]
Reasons added: test1 (Type), some test 2 (Type), test3 , and this would be the end"


SQL> select trim(substr(col,
  2                instr(col, 'Reasons added') + 14,
  3                --
  4                case when instr(col, 'Reasons removed') > 0 then
  5                  instr(col, 'Reasons removed') - instr(col, 'Reasons added') - 14
  6                else
  7                  length(col)
  8                end)) result
  9  from test;

RESULT
--------------------------------------------------------------------------------
test1 (Type), some test 2 (Type), test3 , sometest4& and5(Type)
test1 (Type), some test 2 (Type), test3 , and this would be the end"

SQL>

答案 1 :(得分:0)

一种选择是使用regexp_substr()函数,如下所示:

updateChildSelection(event) {
  // set child to true if checked
   if(event.target.checked){
    this.myVarChild1 = true;
    this.myVarChild2 = true
   } else {
   //set child model to false 
   }

}

Demo