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