| From: | Alvaro Herrera <alvherre(at)2ndquadrant(dot)com> | 
|---|---|
| To: | Marcin Krawczyk <jankes(dot)mk(at)gmail(dot)com> | 
| Cc: | "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgresql(dot)org> | 
| Subject: | Re: regexp_replace behavior | 
| Date: | 2012-11-20 14:55:08 | 
| Message-ID: | 20121120145508.GC3948@alvh.no-ip.org | 
| Views: | Whole Thread | Raw Message | Download mbox | Resend email | 
| Thread: | |
| Lists: | 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
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Marcin Krawczyk | 2012-11-20 15:02:29 | Re: regexp_replace behavior | 
| Previous Message | Marcin Krawczyk | 2012-11-20 14:43:18 | regexp_replace behavior |