pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
regexp_replace behavior
3+ messages / 2 participants
[nested] [flat]

* regexp_replace behavior
@ 2012-11-20 14:43  Marcin Krawczyk <jankes.mk@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Marcin Krawczyk @ 2012-11-20 14:43 UTC (permalink / raw)
  To: pgsql-sql

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"
what am I missing ?

regards
mk

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: regexp_replace behavior
@ 2012-11-20 14:55  Alvaro Herrera <alvherre@2ndquadrant.com>
  parent: Marcin Krawczyk <jankes.mk@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Alvaro Herrera @ 2012-11-20 14:55 UTC (permalink / raw)
  To: Marcin Krawczyk <jankes.mk@gmail.com>; +Cc: pgsql-sql

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




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: regexp_replace behavior
@ 2012-11-20 15:02  Marcin Krawczyk <jankes.mk@gmail.com>
  parent: Alvaro Herrera <alvherre@2ndquadrant.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Marcin Krawczyk @ 2012-11-20 15:02 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: pgsql-sql

Yes that's exactly what I needed. Thanks a lot.

pozdrowienia
mk


2012/11/20 Alvaro Herrera <alvherre@2ndquadrant.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
>

^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2012-11-20 15:02 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2012-11-20 14:43 regexp_replace behavior Marcin Krawczyk <jankes.mk@gmail.com>
2012-11-20 14:55 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2012-11-20 15:02   ` Marcin Krawczyk <jankes.mk@gmail.com>

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