Regex

Started by MaRCeLO PeReiRAabout 20 years ago3 messagesgeneral
Jump to latest
#1MaRCeLO PeReiRA
gandalf_mp@yahoo.com.br

Hi All,

Is there a way to execute a regex, inside a SELECT
statement, to substitute things?

For example:

SELECT name FROM mytable;
name
---------
john
michael
robert
richard
chandler

I want to substitute all "r" to "-"

SELECT myfunction(name) FROM mytable;
name
---------
john
michael
-obert
-icha-d
chandle-

Any idea??

Thanks in advance,
Regards,

Marcelo P.
Brazil

_______________________________________________________
Yahoo! Acesso Gr�tis - Internet r�pida e gr�tis. Instale o discador agora!
http://br.acesso.yahoo.com

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: MaRCeLO PeReiRA (#1)
Re: Regex

MaRCeLO PeReiRA <gandalf_mp@yahoo.com.br> writes:

Is there a way to execute a regex, inside a SELECT
statement, to substitute things?

There's a regex_replace() function in recent PG versions. Or you could
write a function in plperl or pltcl to use the regex capabilities of
those languages.

regards, tom lane

#3rlee0001
robeddielee@hotmail.com
In reply to: Tom Lane (#2)
Re: Regex

If your version does not support regexp_replace(), I have written a
similar function for easlier versions of postgresql using pl/pgsql
called regexp_replacex(). You can find it by searching google groups.
As the thread there points out, the function I wrote doesn't treat
NULLs properly as posted and there are probably other issues as well.
You are free to use it anyways at your own risk.

If your version of PostgreSQL does support the regexp_replace()
function then you should use it instead as Tom Lane pointed out.

-Robert