Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

REGEXP_REPLACE how to replace multiple words delimited by single space?

garbuyaOct 3 2018 — edited Oct 22 2018

Very simple example:

select regexp_replace('This is the place for them to exist, isn''t it?', '(^| )((the)|(is))( |$)', '\1!\5', 1, 0, 'i')rr FROM dual;

This ! the place for them to exist, isn't it?

As you can see, only "is" is replaced and "the" stays as is.

Adding "x" to the match parameter does not help because it replaces all occurrences of "the" and "is" instead replacing only words

select regexp_replace('This is the place for them to be, isn''t it?', '(^| )((the)|(is))( |$)', '\1!\5', 1, 0, 'ix')rr FROM dual;

Th! ! ! place for !m to ex!t, !n't it?

So, the only way I know is to double spaces and then dedup it

select replace(regexp_replace(replace('this is the place for them to exist, isn''t it?',' ','  ') , '(^| )((the)|(is))( |$)', '\1!\5', 1, 0, 'i'),'  ',' ')rr from dual;

this ! ! place for them to exist, isn't it?

Is there a way to do it without manipulating spaces??

This post has been answered by Paulzip on Oct 3 2018
Jump to Answer

Comments

Post Details

Added on Oct 3 2018
11 comments
26,856 views