Replacing characters in a string

Started by Luís de Sousaover 15 years ago8 messagesgeneral
Jump to latest
#1Luís de Sousa
luis.a.de.sousa@gmail.com

Hello everyone,

I need to replace all occurrences of a certain character in a string.
For that I'm using regexp_replace, but so far I only managed to
replace the first character, here's an example:

SELECT regexp_replace('xaxx', 'x', 'e');

regexp_replace
----------------
eaxx
(1 row)

But the result I'd need is 'eaee'. How can I do it?

Thanks,

Luís

#2Thom Brown
thom@linux.com
In reply to: Luís de Sousa (#1)
Re: Replacing characters in a string

2010/9/14 Luís de Sousa <luis.a.de.sousa@gmail.com>:

Hello everyone,

I need to replace all occurrences of a certain character in a string.
For that I'm using regexp_replace, but so far I only managed to
replace the first character, here's an example:

SELECT regexp_replace('xaxx', 'x', 'e');

 regexp_replace
----------------
 eaxx
(1 row)

But the result I'd need is 'eaee'. How can I do it?

Just remove the "regexp" bit:

SELECT replace('xaxx', 'x', 'e');

--
Thom Brown
Twitter: @darkixion
IRC (freenode): dark_ixion
Registered Linux user: #516935

#3Szymon Guz
mabewlun@gmail.com
In reply to: Luís de Sousa (#1)
Re: Replacing characters in a string

2010/9/14 Luís de Sousa <luis.a.de.sousa@gmail.com>

Hello everyone,

I need to replace all occurrences of a certain character in a string.
For that I'm using regexp_replace, but so far I only managed to
replace the first character, here's an example:

SELECT regexp_replace('xaxx', 'x', 'e');

regexp_replace
----------------
eaxx
(1 row)

But the result I'd need is 'eaee'. How can I do it?

Hi,
try this one:

SELECT regexp_replace('xaxx', 'x', 'e', 'g');

regards
Szymon Guz

#4Pavel Stehule
pavel.stehule@gmail.com
In reply to: Luís de Sousa (#1)
Re: Replacing characters in a string

Hello

2010/9/14 Luís de Sousa <luis.a.de.sousa@gmail.com>:

Hello everyone,

I need to replace all occurrences of a certain character in a string.
For that I'm using regexp_replace, but so far I only managed to
replace the first character, here's an example:

SELECT regexp_replace('xaxx', 'x', 'e');

 regexp_replace
----------------
 eaxx
(1 row)

But the result I'd need is 'eaee'. How can I do it?

postgres=# select replace('abcdeabcde','a','x');
replace
────────────
xbcdexbcde
(1 row)

or

postgres=# SELECT regexp_replace('xaxx', 'x', 'e','g'); -- use a flag Global
regexp_replace
────────────────
eaee
(1 row)

Regards

Pavel Stehule

Show quoted text

Thanks,

Luís

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#5Sergey Konoplev
gray.ru@gmail.com
In reply to: Luís de Sousa (#1)
Re: Replacing characters in a string

2010/9/14 Luís de Sousa <luis.a.de.sousa@gmail.com>:

SELECT regexp_replace('xaxx', 'x', 'e');

 regexp_replace
----------------
 eaxx
(1 row)

But the result I'd need is 'eaee'. How can I do it?

Just specify 'g' as the flags parameter (the 4th one). It means 'globally'.

SELECT regexp_replace('xaxx', 'x', 'e', 'g');

There are more of this flags described here:
http://www.postgresql.org/docs/8.4/interactive/functions-matching.html#POSIX-EMBEDDED-OPTIONS-TABLE

p.s. The question is for hackers - BTW I did not find 'g' in this
table, is it a docs bug?

Thanks,

Luís

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

--
Sergey Konoplev

Blog: http://gray-hemp.blogspot.com /
Linkedin: http://ru.linkedin.com/in/grayhemp /
JID/GTalk: gray.ru@gmail.com / Skype: gray-hemp / ICQ: 29353802

#6Michele Petrazzo - Unipex
michele.petrazzo@unipex.it
In reply to: Luís de Sousa (#1)
Re: Replacing characters in a string

Luís de Sousa ha scritto:

Hello everyone,

I need to replace all occurrences of a certain character in a string.
For that I'm using regexp_replace, but so far I only managed to
replace the first character, here's an example:

SELECT regexp_replace('xaxx', 'x', 'e');

regexp_replace
----------------
eaxx
(1 row)

But the result I'd need is 'eaee'. How can I do it?

Using the right function? :)

test=# SELECT replace('xaxx', 'x', 'e');
replace
---------
eaee
(1 row)

Your usage involving regexp!

P.s. google for "replace string postgresql" and I'm feeling lucky

Thanks,

Luís

Michele

#7Luís de Sousa
luis.a.de.sousa@gmail.com
In reply to: Michele Petrazzo - Unipex (#6)
Re: Replacing characters in a string

Thank you for all the answers, several ways this can be made.

Luís

#8Tom Lane
tgl@sss.pgh.pa.us
In reply to: Sergey Konoplev (#5)
Re: Replacing characters in a string

Sergey Konoplev <gray.ru@gmail.com> writes:

Just specify 'g' as the flags parameter (the 4th one). It means 'globally'.

SELECT regexp_replace('xaxx', 'x', 'e', 'g');

There are more of this flags described here:
http://www.postgresql.org/docs/8.4/interactive/functions-matching.html#POSIX-EMBEDDED-OPTIONS-TABLE

p.s. The question is for hackers - BTW I did not find 'g' in this
table, is it a docs bug?

No. That table is referenced for numerous cases where 'g' is not an
allowed flag. Instead, 'g' is documented in-line in the description
of regexp_replace.

regards, tom lane