1

I'm using Oracle 10g trying to EXCLUDE entries that contain a - or a _ with a caret in the character class in Oracle 10g. I can find the entries containing dash or underscore through:

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, '[_\-]')

I know I can get by with using NOT but how do I negate this with a caret (^)? I've tried [^_\-] which returns everything, [^[_\-]] which returns nothing, and [^(_\-)] which is invalid.

Ben
  • 51,770
  • 36
  • 127
  • 149
mbow
  • 147
  • 1
  • 3
  • 11
  • You might find http://www.regexper.com/ useful for trying out the answers. It gives you a visual representation of the regex and has saved me hours – Dave May 17 '13 at 16:18

3 Answers3

1

Try:

^[^_-]*$

I believe that [^_-] matches anything, because it is looking for Any character that is anything other than '_' or '-'. Similar to the opposite that works, [_-], which finds any character, anywhere in the string, that is either a '-' or '_'.

To change that, accept any number of character matching you character class [^_-], and surround with ^ (start of line) and $ (end of line).

femtoRgon
  • 32,893
  • 7
  • 60
  • 87
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 ;-)

tbone
  • 15,107
  • 3
  • 33
  • 40
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, '^[^_-]*$')

fiddle

Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64