REGEXP_REPLACE - can the ReplaceStr be a regexp also?

Database
New Member

REGEXP_REPLACE - can the ReplaceStr be a regexp also?

Hello,

I am trying to use REGEXP_REPLACE function to replace each word of input string with the calculated value of the abbreviation - max. 3 first letters of a word.

E.g. 'Jedi Knight' -> 'JedKni'

'I like Star Wars' -> 'IlikStaWar'

The problem I encountered is, how to use regexp also in ReplaceStr part of the function?

I can put a string value here: select REGEXP_REPLACE('Jedi Knight', '[A-Za-z0-9]{1,}', 'Obi') - it will result in changing 'Jedi Knight' into 'Obi Obi' string.

But I have problems with putting here any regexp (instead of 'Obi').

 

I hope you can help me.

 

Regards,

Maati

1 REPLY
Highlighted
Junior Contributor

Re: REGEXP_REPLACE - can the ReplaceStr be a regexp also?

This matches your description by defining two capturing groups ():

 

RegExp_Replace('I like Star Wars', '([A-Za-z0-9]{1,3})([A-Za-z0-9]* ?)','\1',1,0)

 

group 1 - ([A-Za-z0-9]{1,3}): up to three characters

group 2 - ([A-Za-z0-9]* ?): optional additional characters followed by an optional space.

 

The replace part '\1' keeps group 1, but drops group 2.