I am trying to replace two dots following each other with Null.
SQL> select regexp_replace('..','[^\.]+\.','Null') from dual;
produces:
RE
--
..
whereas I want:
RE
--
Null
How do I achieve this using Regex?
Try the following:
SELECT regexp_replace( '..', '\.{2}', 'Null' ) FROM dual;
Your example is using the ^ symbol for negation ("do not match a ."), which I'm not sure is your intention. If you are instead wanting to match .. only at the beginning of a string, use ^ as follows:
SELECT regexp_replace( '..', '^\.{2}', 'Null' ) FROM dual;
This is going to look a little hairy but for the regex parameter:
(?!\.{3,})\.{2}(?<!\.{3,})
That will make sure there's not any more or any less than 2 .. following each other, from either the front or the back.
So it'll only match .., not ..., not ., etc. I'm assuming this is what you want.
If you're looking for two dots at the beginning of a line, perhaps the simplest expression which could possibly work is
SELECT REGEXP_REPLACE('..', '^\.\.', 'Null') FROM DUAL
If you're looking for two dots anywhere in the line remove the ^ at the beginning of the expression.
If you're looking for two dots which comprise the entire line, add $ at the end of the expression, as in
'^\.\.$'
Documentation for regular expression elements supported by Oracle can be found here.
Share and enjoy.