Question

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.

Was it helpful?

Solution 2

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

OTHER TIPS

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).

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 ;-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top