如何使用正则表达式识别特定模式之间的单词:Oracle?



我有一个文本字段。我需要识别模式<a hrefa>之间的单词。

此模式可以位于文本的开头/结尾/中间。

with t as (
select '<a href Part of the technical Network Group www.tech.com/sites/ hh a>' as text from dual
union select '<a href www.tech.technical Network a>' as text from dual union
select 'www.tech.tech///technical <a href Network Group a>' as text from dual)
select * from t
WHERE REGEXP_LIKE(text,'(^|W)<a hrefS*','i') 

这给了我前 2 行的结果,这是正确的。但是我需要检查单词"组"(不区分大小写(。我们如何检查"组"一词以及该词应该在模式中。在这种情况下,应返回第 1 行和第 3 行。

搜索完整的模式,然后在该模式的子字符串中搜索单词Group。如果文本中有多个匹配项,则可以使用递归子查询分解子句来查找每个匹配项:

甲骨文设置

CREATE TABLE table_name ( id, text ) AS
select 1, '<a href Part of the technical Network Group www.tech.com/sites/ hh a>' from dual union all
select 2, '<a href www.tech.technical Network a>' from dual union all
select 3, 'www.tech.tech///technical <a href Network Group a>' from dual union all
select 4, '<a hrefgroup a>' FROM DUAL UNION ALL
select 5, '<a href groupa>' FROM DUAL UNION ALL
select 6, '<a href workgroup a>' FROM DUAL UNION ALL
select 7, '<a href test1 a> Group <a href test2 a>' FROM DUAL;

查询

WITH positions ( id, text, match, position ) AS (
SELECT id,
text,
REGEXP_SUBSTR(
text,
'(^|W)<a hrefs+.*?s+a>(W|$)',
1,
1,
'i'
),
REGEXP_INSTR(
text,
'(^|W)<a hrefs+.*?s+a>(W|$)',
1,
1,
0,
'i'
)
FROM   table_name
UNION ALL
SELECT id,
text,
REGEXP_SUBSTR(
text,
'(^|W)<a hrefs+.*?s+a>(W|$)',
position + 1,
1,
'i'
),
REGEXP_INSTR(
text,
'(^|W)<a hrefs+.*?s+a>(W|$)',
position + 1,
1,
0,
'i'
)
FROM   positions
WHERE  position > 0
)
SELECT id,
text
FROM   positions
WHERE  REGEXP_LIKE( match, 'sGroups', 'i' );

输出

身份证 |发短信                                                                 -: |:--------------------------------------------------------------------  1 |  3 |www.tech.tech///technical

db<>fiddle here

你可以像这样扩展正则表达式:<a href.*group.*a>.

DB小提琴上的演示

with t as (
select '<a href Part of the technical Network Group www.tech.com/sites/ hh a>' as text from dual
union all select '<a href www.tech.technical Network a>' as text from dual
union all select 'www.tech.tech///technical <a href Network Group a>' as text from dual)
select * from t
WHERE REGEXP_LIKE(text,'<a href.*group.*a>','i') 
|正文 | |:-------------------------------------------------------------------- | | | |www.tech.tech///technical
							

相关内容

最新更新