Re: Find and replace

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Campbell, Lance" <lance(at)illinois(dot)edu>
Cc: "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Find and replace
Date: 2019-09-11 20:49:35
Message-ID: 18540.1568234975@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

"Campbell, Lance" <lance(at)illinois(dot)edu> writes:
> I don’t know the best way to do this. I need to do a find and replace in text fields. The value I need to find and replace may occur more than once in each field per record.

> The value I am trying to match on:
> Starts with a single { .
> Ends with a single } .
> In between these brackets can be the characters 0-9, a-z, A-Z, hyphens and underscores. But no spaces. These characters could be in any order.
> The replacement value on a match is the same as what was found except for double {{ at the beginning and double }} at the end. Same values between the brackets as what was matched on.

Sounds like a job for regular expressions.

regression=# select regexp_replace('abc{foo1}def{goo_bug}a', '{([-_a-zA-Z0-9]*)}', '{{\1}}', 'g');
regexp_replace
----------------------------
abc{{foo1}}def{{goo_bug}}a
(1 row)

See
https://www.postgresql.org/docs/current/functions-matching.html#FUNCTIONS-POSIX-REGEXP

regards, tom lane

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Karen Goh 2019-09-12 10:50:19 Re: How do I enabled Windows 10 to be able to run PSQL etc
Previous Message Campbell, Lance 2019-09-11 20:31:09 Re: Find and replace