0

我正在使用Oracle 12cR1。我需要一个 REGEX 模式来完全匹配 Oracle 源代码中的给定表名(即)匹配不能在数字、字母或下划线符号之前或之后。

我创建了一个虚拟源代码并尝试了以下内容。它没有获取所有所需的记录。我错过了什么?是否有一个正则表达式模式?

测试代码:

with names as (
select 'mytabname' tabname from dual union all
select 'mytabname1' from dual union all
select 'mytabname_' from dual union all
select 'mytabnameX' from dual union all
select '1mytabname' from dual union all
select '_mytabname' from dual union all
select 'Xmytabname' from dual union all
select '1mytabname_' from dual union all
select '1mytabnameX' from dual union all
select '_mytabname1' from dual union all
select 'Xmytabname1' from dual union all
select '_mytabnameX' from dual union all
select 'Xmytabname_' from dual union all
select '_mytabname_' from dual union all
select 'XmytabnameX' from dual union all
select '1mytabname1' from dual union all
select ' mytabname_' from dual union all
select '_mytabname ' from dual union all
select ' mytabname ' from dual union all
select ' mytabnameX' from dual union all
select 'Xmytabname ' from dual union all
select ' mytabname1' from dual union all
select '1mytabname ' from dual union all
select '.mytabname ' from dual union all
select ' mytabname.' from dual union all
select '.mytabname.' from dual union all
select '*mytabname' from dual union all
select ' mytabname*' from dual union all
select '*mytabname*' from dual union all
select 'defmytabnameabc' from dual)
select 'start'||tabname||'end' from names
where regexp_like(tabname, '[^a-zA-Z0-9_](mytabname)[^a-zA-Z0-9_]');

输出:

start mytabname end
start.mytabname end
start mytabname.end
start.mytabname.end
start mytabname*end
start*mytabname*end

输出遗漏以下记录

startmytabnameend
start*mytabnameend
4

1 回答 1

1

我一般用这个:

SELECT *
  FROM user_source
 WHERE regexp_like(text, '(\W|^)' || 'employees' || '(\W|$)', 'i');

在这里找到正则表达式的详细信息: https ://stackoverflow.com/a/52441424/7998591

'i'用于忽略大小写,如果您愿意,可以省略。

数据演示

于 2018-10-24T15:10:15.063 回答