Search code examples
sqlregexoracle-databaseregexp-replace

PLSQL - help on my regex phone number with area code


I have difficulties to find the right regex under PL/SQL, but my regex is normally good

I have a phone number like this :

+44 (0)22 3333 4444 from the text that should not be there

And I want to get this:

+4402233334444

So I made the following regex:

/[^+\d]|\s/g

It works very well on the site https://regexr.com/ but not in my PL/SQL query, it gives me the same result

I tried to use the oracle doc, but without success https://www.techonthenet.com/oracle/regexp_like.php


Solution

  • The \d and other shorthand character classes should not be used inside a bracket expression.

    You can use

    SELECT 
      REGEXP_REPLACE(
        '+44 (0)22 3333 4444',
        '[^+0-9]',
        ''
    ) As Result FROM dual;
    

    where [^+0-9] matches any char other than + and a digit.

    See the DB fiddle.

    Note that [^+0-9] already matches any whitespace chars since non-digit chars other than + also match what \s matches, so you can safely omit the |\s from your regex.