Re: Find and replace - Mailing list pgsql-sql

From Tom Lane
Subject Re: Find and replace
Date
Msg-id 18540.1568234975@sss.pgh.pa.us
Whole thread Raw
In response to Find and replace  ("Campbell, Lance" <lance@illinois.edu>)
List pgsql-sql
"Campbell, Lance" <lance@illinois.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
replacemay 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
characterscould 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
theend.  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



pgsql-sql by date:

Previous
From: "Campbell, Lance"
Date:
Subject: Re: Find and replace
Next
From: Karen Goh
Date:
Subject: Re: How do I enabled Windows 10 to be able to run PSQL etc