1

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?

3 Answers 3

3

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;
Sign up to request clarification or add additional context in comments.

3 Comments

Brilliant not sure why mine didnt work and strange neither this works select regexp_replace('..','[^\.]{2}','Null') from dual;
@jhon.smith: ^ has two distinct uses in Regex: 1) for negation inside a character set (which is how you're using it) and 2) to match the beginning of a string. [^\.]{2} instructs Regex to "match two consecutive NON-DOT characters", whereas ^[\.]{2} says "match two dots at the beginning of a string".
Thanks brilliant explanation as well why mine did not work :-)
0

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.

1 Comment

Heck Jason yes you hacked my mind no computer on the universe can read my mind and give me an answer
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.