首页 > 解决方案 > 在Oracle的文本中找到单词时如何获取单词的位置?

问题描述

该表有单词和句子。如果找到单词,如何获得它的位置?temp.word 下面既有信息安全又有信息。对于 id = 100,我希望在第二次出现时看到该信息(有关更多信息),因为我们在 temp.word 中具有信息安全性。所以,我们需要跳过信息安全。下面的代码选择首先出现的信息。

Create table temp(
  id       NUMBER,
  word     VARCHAR2(1000),
  Sentence VARCHAR2(2000)
);

insert into temp 
select 100,'Information Security','Information security, sometimes shortened to infosec. For more information, visit https://infosec.com' FROM DUAL UNION ALL
select 200,'Information','Information-security information.' FROM DUAL UNION ALL
select 300,'Info','https://infosec.com Information Security' FROM DUAL;

select word,sentence, regexp_instr(sentence,word,1, 1, 0,'i') as token
from temp
where instr(UPPER(sentence), UPPER('information')) > 0
and regexp_like(sentence,'information','i')

标签: sqlregexoracle

解决方案


要查找第二次出现,您可以使用regexp_substr.

它的语法如下:

REGEXP_INSTR(字符串,模式 [,start_position [,nth_appearance [,return_option [,match_parameter [,sub_expression]]]]])

在您的情况下,使用 2nth_appearance如下

regexp_instr(sentence,word,1, 2, 0,'i') as token

Db<>小提琴演示

干杯!!


推荐阅读