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.

Reg Exp - not as expected

BluShadowOct 31 2007 — edited Nov 1 2007
Not often I ask questions myself, and perhaps my mind's just gone fuzzy this morning, but I'm having trouble doing a simple replace with regular expressions...

In the below example I just want to replace all occurences of "fred" with "freddies"...
SQL> ed
Wrote file afiedt.buf

  1* select regexp_replace('this freddies is fred fred record', 'fred', 'freddies') from dual
SQL> /

REGEXP_REPLACE('THISFREDDIESISFREDFREDRECORD'
---------------------------------------------
this freddiesdies is freddies freddies record
but, obviously, I don't want the existing "freddies" to become "freddiesdies", it should stay as it is. So if I check for spaces either side of the search string (and take account of it maybe being at the start/end of the string...
SQL> ed
Wrote file afiedt.buf

  1* select regexp_replace('this freddies is fred fred record', '(^| )fred( |$)', '\1freddies\2') from dual
SQL> /

REGEXP_REPLACE('THISFREDDIESISFREDFRE
-------------------------------------
this freddies is freddies fred record

SQL>
It no longer replaces the "freddies" incorrectly, BUT it only replaces the first occurence of "fred" with "freddies" and not the latter one. ?!?!?!

If I put an extra space inbetween the two "fred"s then it works:
SQL> ed
Wrote file afiedt.buf

  1* select regexp_replace('this freddies is fred  fred record', '(^| )fred( |$)', '\1freddies\2',2) from dual
SQL> /

REGEXP_REPLACE('THISFREDDIESISFREDFREDRECO
------------------------------------------
this freddies is freddies  freddies record

SQL>
But I'm not going to have double spaces between the words and I can't determine where to insert them to make it work like this so it's not a feasible solution, although it does seem to highlight that the regular expression parser is not taking the space after the first match in the context of being the space before the second match.

Is that a bug in the regular expression parser, a feature of the way regular expressions work (i.e. expected) or am I doing something wrong with my search and replace reg.exp. strings?

Cheers

Comments

Processing
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Nov 29 2007
Added on Oct 31 2007
17 comments
2,677 views