pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alvaro Herrera <alvherre@2ndquadrant.com>
To: Marcin Krawczyk <jankes.mk@gmail.com>
Cc: pgsql-sql@postgresql.org <pgsql-sql@postgresql.org>
Subject: Re: regexp_replace behavior
Date: Tue, 20 Nov 2012 11:55:08 -0300
Message-ID: <20121120145508.GC3948@alvh.no-ip.org> (raw)
In-Reply-To: <CABnqL301mDLD5UwXfLcUprk2EMt7c5JzJKJz_icAHyx3fN61Vg@mail.gmail.com>
References: <CABnqL301mDLD5UwXfLcUprk2EMt7c5JzJKJz_icAHyx3fN61Vg@mail.gmail.com>

Marcin Krawczyk escribió:
> Hi list,
> 
> I'm trying to use regexp_replace to get rid of all occurrences of
> certain sub strings from my string.
> What I'm doing is:
> 
> SELECT regexp_replace('F0301 305-149-101-0 F0302 {x1} 12W47 0635H
> {tt}{POL23423423}', E'\{.+\}', '', 'g')
> 
> so get rid of whatever is between { } along with these,
> 
> but it results in:
> 'F0301 305-149-101-0 F0302 '
> 
> how do I get it to be:
> 'F0301 305-149-101-0 F0302 12W47 0635H'
> 
> ??
> 
> as I understood the docs, the g flag "specifies replacement of each
> matching substring rather than only the first one"

The first \{.+\} match starts at the first { and ends at the last },
eating the {s and }s in the middle.  So there's only one match and that's
what's removed.

> what am I missing ?

You need a non-greedy quantifier.  Try

 SELECT regexp_replace('F0301 305-149-101-0 F0302 {x1} 12W47 0635H {tt}{POL23423423}', E'\{.+?\}', '', 'g')

-- 
Álvaro Herrera                http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services




view thread (3+ messages)  latest in thread

Message-ID: <20121120145508.GC3948@alvh.no-ip.org>
Permalink:  ../20121120145508.GC3948@alvh.no-ip.org/
Also on:    postgresql.org/message-id/20121120145508.GC3948@alvh.no-ip.org

 · 

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: alvherre@2ndquadrant.com, jankes.mk@gmail.com
  Subject: Re: regexp_replace behavior
  In-Reply-To: <20121120145508.GC3948@alvh.no-ip.org>

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

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