By default, in PostgreSQL, regexp_replace()
replaces the first occurrence of a substring in a string, while replace()
replaces all occurrences of a substring in a string. Why is the default behavior different?
(I know that with regexp_replace
one can use the g
option to replace all occurrences of a substring in a string.)
Examples:
SELECT regexp_replace('hello world', 'o', 'z'); -- returns "hellz world"
SELECT regexp_replace('hello world', 'o', 'z', 'g'); -- returns "hellz wzrld"
SELECT replace('hello world', 'o', 'z'); -- returns: "hellz wzrld"
1 Answer 1
Why?
Because Postgres replace()
is a standard SQL function that works the same as in other RDBMS. Example: replace()
in SQL Server:
Replaces all occurrences of a specified string value with another string value.
While regexp_replace()
is used to ...
Replace substring(s) matching a POSIX regular expression.
The handling of regular expressions is obviously guided by the POSIX standard and works the same as other tools implementing it. The manual:
POSIX regular expressions provide a more powerful means for pattern matching than the
LIKE
andSIMILAR TO
operators. Many Unix tools such asegrep
,sed
, orawk
use a pattern matching language that is similar to the one described here.
Explore related questions
See similar questions with these tags.
regexp_replace
is same asreplace
,replace
is not needed. Additionally,g
option is the same behavior of Unix tools that operate regular expressions such assed
.replace()
in question. Now it should make sense.