Re: Find and replace
Tom Lane <[email protected]> Wed, 11 Sep 2019 16:49:35 -0400
| Newsgroups | gmane.comp.db.postgresql.sql |
|---|---|
| Message-ID | <[email protected]> |
"Campbell, Lance" <[email protected]> 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