oracle中字符串中第n次出现后的子串

Substring after nth occurrence of substring in a string in oracle

我想从 oracle db 中提取这样的字符串:

12/10 10:43:32: - 170000 subs exported<br />12/10 10:43:32: - 175000 subs exported<br />12/10 10:43:32: - 180000 subs exported<br />12/10 10:43:32: - 185000 subs exported<br />12/10 10:43:32: - 190000 subs exported<br />12/10 10:43:32: - 195000 subs exported<br />12/10 10:43:32: - 200000 subs exported<br />12/10 10:43:32: - 200400 subs exported<br /> 12 / 10 10 :43 :32 :processing completed successfully

到最后 3 行:

12/10 10:43:32: - 200000 subs exported<br />12/10 10:43:32: - 200400 subs exported<br /> 12 / 10 10 :43 :32 :processing completed successfully

我试过这个: substr(string, instr(string, '<br />', 1, 1)+2)

但在某些情况下,我不知道有多少 <br />,所以我发现很难找到最后 3 行的开始索引。 (该列数据类型为CLOB)

这可能是一种选择:

  • regexp_count 查找列中 <br /> 字符串的数量(因此您必须从该数字中减去 2 以获得最后 3
  • 剩下的就简单了;使用从先前计算的位置开始的 SUBSTR 和 return 列值的其余部分

SQL> select
  2    substr(col, instr(col, '<br />', 1, regexp_count(col, '<br />') - 2) + 6) result
  3  from test;

RESULT
--------------------------------------------------------------------------------
12/10 10:43:32:  - 200000 subs exported<br />12/10 10:43:32:  - 200400 subs expo

SQL>

在 Oracle 中,您还可以通过指定负值(即 -1)从字符串 向后 的末尾算起第 3 次出现 'br />'对于 INSTR 的第三个参数和您想要的出现次数(即 3)作为第四个参数。例如

SELECT SUBSTR(mytext, INSTR(mytext, '<br />', -1, 3) + 6) AS answer FROM test

这也应该给你最后 3 行。