Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to tell oracle SQL the backslash in regex is no escape symbol

The query select regexp_replace('Wd+Wd4Wd', '[\W\d]+', '_') from dual outputs _+_4_, meaning it treats the backslash as escape character and replaces W and d instead of + and 4. It works if \W and \d are not used in brackets: select regexp_replace('Wd+Wd4Wd', '(\W|\d)+', '_') from dual outputs Wd_Wd_Wd as expected.

How can I match non-word characters and digits within brackets?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use POSIX character classes. For this concrete scenario, you can use

select regexp_replace('Wd+Wd4Wd', '[[:digit:][:punct:][:space:]]+', '_') from dual

To match digits, [:digit:] or 0-9 can work.

As for non-word characters, I think you just want to match punctuation and whitespace, so [:punct:] and [:space:] POSIX character classes should be enough.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading