The thing is that \d*
can match an empty string and you are not passing the flag g
.
See regexp_replace
:
The flags parameter is an optional text string containing zero or more single-letter flags that change the function's behavior. Flag i
specifies case-insensitive matching, while flag g
specifies replacement of each matching substring rather than only the first one.
The \d*
matches the empty location at the beginning of the JamesBond007
string, and since g
is not passed, that empty string is replaced with 008
when you use select regexp_replace('JamesBond007','\d*','008');
and the result is expected - 008JamesBond007
.
With select regexp_replace('JamesBond007','\d*','');
, again, \d*
matches the empty location at the beginning of the string, and replaces it with an empty string (no visible changes).
Note that Oracle's REGEXP_REPLACE
replaces all occurrences by default:
By default, the function returns source_char
with every occurrence of the regular expression pattern replaced with replace_string
.
In general, you should be cautious when using patterns matching empty strings inside regex-based replace functions/methods. Do it only when you understand what you are doing. If you want to replace digit(s) you usually want to find at least 1 digit. Else, why remove something that is not present in the string in the first place?