agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Campbell, Lance <lance@illinois.edu>
Cc: pgsql-sql@postgresql.org <pgsql-sql@postgresql.org>
Subject: Re: Find and replace
Date: Wed, 11 Sep 2019 16:49:35 -0400
Message-ID: <18540.1568234975@sss.pgh.pa.us> (raw)
In-Reply-To: <35490FCC-14AF-4D90-BF6E-A4F00518E41E@illinois.edu>
References: <35490FCC-14AF-4D90-BF6E-A4F00518E41E@illinois.edu>

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





view thread (4+ messages)

Message-ID: <18540.1568234975@sss.pgh.pa.us>
Permalink:  ../18540.1568234975@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/18540.1568234975@sss.pgh.pa.us

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, lance@illinois.edu
  Subject: Re: Find and replace
  In-Reply-To: <18540.1568234975@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox