3

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

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

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 href\S*','i') 

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

4

2 回答 2

1

搜索完整的模式,然后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 href\s+.*?\s+a>(\W|$)',
           1,
           1,
           'i'
         ),
         REGEXP_INSTR(
           text,
           '(^|\W)<a href\s+.*?\s+a>(\W|$)',
           1,
           1,
           0,
           'i'
         )
  FROM   table_name
UNION ALL
  SELECT id,
         text,
         REGEXP_SUBSTR(
           text,
           '(^|\W)<a href\s+.*?\s+a>(\W|$)',
           position + 1,
           1,
           'i'
         ),
         REGEXP_INSTR(
           text,
           '(^|\W)<a href\s+.*?\s+a>(\W|$)',
           position + 1,
           1,
           0,
           'i'
         )
  FROM   positions
  WHERE  position > 0
)
SELECT id,
       text
FROM   positions
WHERE  REGEXP_LIKE( match, '\sGroup\s', 'i' );

输出

身份证 | 文本                                                                 
-: | :------------------------------------------------ ------------------
 1 | <a href 属于技术网络组 www.tech.com/sites/ hh a>
 3 | www.tech.tech///technical <a href 网络组 a>                   

db<>在这里摆弄

于 2019-09-27T19:52:52.927 回答
1

您可以扩展您的正则表达式,例如:<a href.*group.*a>

DB Fiddle 上的演示

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') 
| 正文 |
| :------------------------------------------------ ------------------- |
| <a href 属于技术网络组 www.tech.com/sites/ hh a> |
| www.tech.tech///technical <a href 网络组a> |

注意:只要您的文本仅包含一种<a href ... a>模式(示例数据中就是这种情况),这就会起作用。


您可以改进正则表达式以确保它仅匹配单词'group'(而不匹配包含'group'、like'workgroup'或的其他单词'grouped'):

<a href.*\sgroup\s.*a>

只要<a href后面总是有一个空格并且a>总是在前面有一个空格,这就会起作用。

DB Fiddle 上的演示

于 2019-09-27T20:05:00.907 回答