Hi Tom,
Here's something I observed and couldn't make anything out of it.
Considering the below two queries: (To summarize, I took a string of 9 repeating characters, and replacing the nth character (3rd in the example) with another character. Ideally replacing a character with another shouldn't alter the length of the original string)
SELECT LENGTH(REGEXP_REPLACE('*********', '*', 'X', 3, 1)) FROM DUAL;
The above query somehow "increases" the length of the original string from 9 to 10, while the below query (essentially the same, just using '-' instead of '*')
SELECT LENGTH(REGEXP_REPLACE('---------', '-', 'X', 3, 1)) FROM DUAL;
"maintains" the length of the original string.
I've tested with multiple other characters, but this discrepancy is happening only with '*'.
Can you help me understand this?
"*" is a special character in REGEX, so you are not *really* replacing the asterisk. To reference a special character (asterisk being an example) you need to escape it.
SQL> SELECT LENGTH(REGEXP_REPLACE('*********', '*', 'X', 3, 1)) FROM DUAL;
SQL> SELECT LENGTH(REGEXP_REPLACE('*********', '\*', 'X', 3, 1)) FROM DUAL;
9
You can see the list of special characters/expressions here
https://docs.oracle.com/database/122/SQLRF/Oracle-Regular-Expression-Support.htm#SQLRF020