Received: from magus.postgresql.org ([87.238.57.229]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TapEK-0007k9-9i for pgsql-sql@postgresql.org; Tue, 20 Nov 2012 14:55:16 +0000 Received: from smtprelay0181.b.hostedemail.com ([64.98.42.181] helo=smtprelay.b.hostedemail.com) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TapEH-0000jx-Nh for pgsql-sql@postgresql.org; Tue, 20 Nov 2012 14:55:15 +0000 Received: from filter.hostedemail.com (b-bigip1 [10.5.19.254]) by smtprelay04.b.hostedemail.com (Postfix) with SMTP id 0143A21736A; Tue, 20 Nov 2012 14:55:12 +0000 (UTC) X-Panda: scanned! X-Spam-Summary: 50, 0, 0, , d41d8cd98f00b204, alvherre@alvh.no-ip.org, :::, RULES_HIT:355:379:599:601:945:967:968:973:988:989:1260:1277:1311:1312:1313:1314:1345:1359:1437:1515:1516:1518:1519:1534:1541:1593:1594:1595:1596:1711:1730:1747:1777:1792:2198:2199:2393:2525:2560:2563:2682:2685:2828:2859:2933:2937:2939:2942:2945:2947:2951:2954:3022:3027:3138:3139:3140:3141:3142:3352:3865:3866:3867:3868:3869:3870:3871:3872:3873:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4250:4321:4605:4659:5007:6119:6261:7679:7903:7904:9010:9025:9121:10004:10400:10848:11256:11257:11658:11914:12296:12517:12519, 0, RBL:none, CacheIP:none, Bayesian:0.5, 0.5, 0.5, Netcheck:none, DomainCache:0, MSF:not bulk, SPF:fn, MSBL:0, DNSBL:none, Custom_rules:0:0:0 X-Session-Marker: 616C76686572726540616C76682E6E6F2D69702E6F7267 X-Filterd-Recvd-Size: 2036 Received: from perhan.alvh.no-ip.org (unknown [190.95.28.153]) (Authenticated sender: alvherre@alvh.no-ip.org) by omf01.b.hostedemail.com (Postfix) with ESMTPA; Tue, 20 Nov 2012 14:55:11 +0000 (UTC) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id 2A5166E0FE; Tue, 20 Nov 2012 11:55:09 -0300 (CLST) Date: Tue, 20 Nov 2012 11:55:08 -0300 From: Alvaro Herrera To: Marcin Krawczyk Cc: "pgsql-sql@postgresql.org" Subject: Re: regexp_replace behavior Message-ID: <20121120145508.GC3948@alvh.no-ip.org> References: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Content-Transfer-Encoding: quoted-printable X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201211/21 X-Sequence-Number: 36954 Marcin Krawczyk escribi=F3: > Hi list, >=20 > 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: >=20 > SELECT regexp_replace('F0301 305-149-101-0 F0302 {x1} 12W47 0635H > {tt}{POL23423423}', E'\{.+\}', '', 'g') >=20 > so get rid of whatever is between { } along with these, >=20 > but it results in: > 'F0301 305-149-101-0 F0302 ' >=20 > how do I get it to be: > 'F0301 305-149-101-0 F0302 12W47 0635H' >=20 > ?? >=20 > 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}{P= OL23423423}', E'\{.+?\}', '', 'g') --=20 =C1lvaro Herrera http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services