1

I'm trying to use the Oracle REGEXP_REPLACE function to replace a whitespace (which is in the middle of a string) with an empty string.

One of my columns contains strings like the following one.

  • [alphanumeric][space][digits][space][alpha] (eg. R4SX 315 GFX)

Now, I need to replace ONLY the second whitespace (the whitespace after the digits) with an empty string (i.e. R4SX 315 GFX --> R4SX 315GFX)

To achieve this, I tried the following code:

SELECT REGEXP_REPLACE(
   'R4SX 315 GFX', 
   '([:alphanum:])\s(\d)\s([:alpha:])', 
   '\1 \2\3') "REPLACED" 
FROM dual;

However, the result that I get is the same as my input (i.e. R4SX 315 GFX). Can someone please tell me what I have done wrong and please point me in the right direction.

Thanks in advance.

2
  • It should be alnum, not alphanum Commented May 27, 2015 at 10:30
  • Thanks for the quick reply. But I tried that before posting this question. Still the same result. :( Commented May 27, 2015 at 10:39

2 Answers 2

2

[:alphanum:]

alphanum is incorrrect. The alphanumeric character class is [[:alnum:]].

You could use the following pattern in the REGEXP_REPLACE:

([[:alnum:]]{4})([[:space:]]{1})([[:digit:]]{3})([[:space:]]{1})([[:alpha:]]{3})

Using REGEXP

SQL> SELECT REGEXP_REPLACE('R4SX 315 GFX',
  2                        '([[:alnum:]]{4})([[:space:]]{1})([[:digit:]]{3})([[:space:]]{1})([[:alpha:]]{3})',
  3                        '\1\2\3\5')
  4  FROM DUAL;

REGEXP_REPL
-----------
R4SX 315GFX

SQL>

If you are not sure about the number of characters in each expression of the pattern, then you could do:

SQL> SELECT REGEXP_REPLACE('R4SX 315 GFX',
  2                        '([[:alnum:]]+[[:blank:]]+[[:digit:]]+)[[:blank:]]+([[:alpha:]]+)',
  3                        '\1\2')
  4  FROM dual;

REGEXP_REPL
-----------
R4SX 315GFX

SQL>

Using SUBSTR and INSTR

The same could be done with substr and instr which wouldbe less resource consuming than regexp.

SQL> WITH DATA AS
  2    ( SELECT 'R4SX 315 GFX' str FROM DUAL
  3    )
  4  SELECT SUBSTR(str, 1, instr(str, ' ', 1, 2) -1)
  5    ||SUBSTR(str, instr(str, ' ', 1, 2)       +1, LENGTH(str)-instr(str, ' ', 1, 2)) new_str
  6  FROM DATA;

NEW_STR
-----------
R4SX 315GFX

SQL>
Sign up to request clarification or add additional context in comments.

1 Comment

@Asela You're welcome! I have added another example using SUBSTR and INSTR.
2

Your regex contains an invalid class alphanum. Also, these classes must be used inside character classes [...]. Instead of \s, you need to use a supported [:blank:] class. More details on the regex syntax in MySQL can be found here.

I recommend using

SELECT REGEXP_REPLACE(
 'R4SX 315 GFX', 
 '([[:alnum:]]+[[:blank:]]+[[:digit:]]+)[[:blank:]]+([[:alpha:]]+)'
 , '\1\2') "REGEXP_REPLACE" 
FROM dual;

This way you will use just 2 capturing groups. The less we have the better is for performance. Here you can see more details on REGEXP_REPLACE function.

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.