Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i89Yc-0007mE-Jk for pgsql-sql@arkaria.postgresql.org; Wed, 11 Sep 2019 20:49:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i89Yb-00063M-34 for pgsql-sql@arkaria.postgresql.org; Wed, 11 Sep 2019 20:49:41 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i89Ya-0005yz-Kn for pgsql-sql@lists.postgresql.org; Wed, 11 Sep 2019 20:49:40 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1i89YX-0004qi-H9 for pgsql-sql@postgresql.org; Wed, 11 Sep 2019 20:49:39 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x8BKnZHh018541; Wed, 11 Sep 2019 16:49:35 -0400 From: Tom Lane To: "Campbell, Lance" cc: "pgsql-sql@postgresql.org" Subject: Re: Find and replace In-reply-to: <35490FCC-14AF-4D90-BF6E-A4F00518E41E@illinois.edu> References: <35490FCC-14AF-4D90-BF6E-A4F00518E41E@illinois.edu> Comments: In-reply-to "Campbell, Lance" message dated "Wed, 11 Sep 2019 20:24:07 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <18539.1568234975.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 11 Sep 2019 16:49:35 -0400 Message-ID: <18540.1568234975@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk "Campbell, Lance" writes: > I don=E2=80=99t know the best way to do this. I need to do a find and r= eplace in text fields. The value I need to find and replace may occur mor= e 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 a= nd 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 fo= r double {{ at the beginning and double }} at the end. Same values betwee= n the brackets as what was matched on. Sounds like a job for regular expressions. regression=3D# 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