Обсуждение: Replacing characters in a string

Поиск
Список
Период
Сортировка

Replacing characters in a string

От
Luís de Sousa
Дата:
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

Re: Replacing characters in a string

От
Thom Brown
Дата:
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

Re: Replacing characters in a string

От
Szymon Guz
Дата:


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 

Re: Replacing characters in a string

От
Pavel Stehule
Дата:
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

>
> 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
>

Re: Replacing characters in a string

От
Sergey Konoplev
Дата:
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

Re: Replacing characters in a string

От
Michele Petrazzo - Unipex
Дата:
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


Re: Replacing characters in a string

От
Luís de Sousa
Дата:
Thank you for all the answers, several ways this can be made.

Luís

Re: Replacing characters in a string

От
Tom Lane
Дата:
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