将单词后的子字符串提取到字符串中的特定单词

时间:2019-11-21 10:20:16

标签: plsql

我有一个字符串“某些单词Host:某些characters.net一些单词”,我想在Host:之后提取子字符串:直到.net,例如,某些characters.net将作为输出

谢谢 拉克什

2 个答案:

答案 0 :(得分:1)

这是一个选择:

SQL> with test (col) as
  2    (select 'some words Host: some characters.net some words'  from dual)
  3  select trim(substr(col,
  4                     instr(col, 'Host:') + 5,
  5                     instr(col, '.net') - instr(col, 'Host:'))
  6             ) result
  7  from test;

RESULT
-------------------
some characters.net

SQL>

答案 1 :(得分:0)

嘿,别忘了REGEXP_SUBSTR()方法!如果找不到匹配项,则返回NULL字符串。

with test (col) as
  (select 'some words Host: some characters.net some words'  from dual)
select regexp_substr(col, 'Host: (.*\.net)', 1, 1, NULL, 1) result
from test;

如果希望在没有匹配项的情况下返回原始字符串,请使用REGEXP_REPLACE():

with test (col) as
      (select 'some words Host: some characters.net some words'  from dual)
    select regexp_replace(col, '.*Host: (.*\.net).*', '\1') result
    from test;