1

我正在使用 Oracle 10g 尝试在 Oracle 10g 的字符类中排除包含 a-或带有插入符号的条目。_我可以通过以下方式找到包含破折号或下划线的条目:

WITH example
     AS (SELECT 'AAAA-1' n FROM DUAL
         UNION
         SELECT 'AAAAA_1' FROM DUAL
         UNION
         SELECT 'AAAA' FROM DUAL)
SELECT *
  FROM example
 WHERE REGEXP_LIKE (n, '[_\-]')

我知道我可以使用 NOT 但我如何用插入符号来否定它(^)?我尝试过[^_\-]返回所有内容, [^[_\-]]什么都不返回,并且[^(_\-)]无效。

4

3 回答 3

1

尝试:

^[^_-]*$

我相信它[^_-]匹配任何东西,因为它正在寻找除“_”或“-”之外的任何字符。与相反的工作类似[_-],它在字符串中的任何位置查找任何字符,即 -”或“_”。

要更改它,请接受与您的 character class 匹配的任意数量的字符,并用(start of line) 和(end of line)[^_-]包围。^$

于 2013-05-17T15:41:42.243 回答
0

I would probably use NOT regexp_like, which is more clear. But since you mentioned you don't want to use NOT, then I would probably use this (again, more clear imo):

select 'Does NOT contain dash or underscore' as val
from dual 
where regexp_instr('ABC123', '[-_]') = 0;

I'm sure you'd have about 20 different regexp versions soon ;-)

If you care about special treatment of empty strings(nulls), use a simple nvl:

nvl(regexp_instr('', '[-_]'),0) = 0; 

I mention this because using regexp_like does not allow for this (nvl isn't a relational operator, but in this case we're comparing instr to a number, so we can use nvl on the instr (left hand) part.

But granted this depends on whether you want to say anything about the existence or non-existence of some characters in a null ;-)

于 2013-05-17T15:50:52.643 回答
0
WITH example
     AS (SELECT 'AAAA-1' n FROM DUAL
         UNION
         SELECT 'AAAAA_1' FROM DUAL
         UNION
         SELECT 'AAAA' FROM DUAL
         UNION
         SELECT 'AAAA\1' FROM DUAL
         )
SELECT *
  FROM example
 WHERE REGEXP_LIKE (n, '^[^_-]*$')

小提琴

于 2013-05-17T15:54:22.803 回答