1

I need to match and replace string like VA123 - so two letters and 3 numbers, but this expression is not working as intended. Any idea where I am going off?

SELECT REGEXP_REPLACE ('test VA123', '^\[A-Z]{2}[0-9]{3}$', 'test')
FROM dual;

I want the output in this case to say test test

6
  • 3
    You need to remove the starting anchor ^, or replace it with word boundary \b Commented Mar 16, 2021 at 0:30
  • 3
    Don't escape [. Commented Mar 16, 2021 at 0:34
  • 3
    @HaoWu Oracle does not support word boundaries (\b). Commented Mar 16, 2021 at 0:38
  • thanks guys! this works SELECT REGEXP_REPLACE ('test VA123', '[A-Z]{2}[0-9]{3}$', 'test') FROM dual; Commented Mar 16, 2021 at 0:39
  • @sarsnake That would work but it would also match ABC123 which might not be what you require. Commented Mar 16, 2021 at 0:42

1 Answer 1

1

It you want white space (or start of a string) before the matched string then you can use:

SELECT REGEXP_REPLACE ('test VA123', '(^|\s)[A-Z]{2}[0-9]{3}$', '\1test')
         AS replaced_value
FROM dual;
| REPLACED_VALUE |
| :------------- |
| test test      |

db<>fiddle here

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

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.