[prev in list] [next in list] [prev in thread] [next in thread]
List: postgresql-sql
Subject: Re: [SQL] regexp_replace behavior
From: Marcin Krawczyk <jankes.mk () gmail ! com>
Date: 2012-11-20 15:02:29
Message-ID: CABnqL329i_eb7fUa3iGpG=_SHpLrkd_wG4hfF1SQosyAqQgzUg () mail ! gmail ! com
[Download RAW message or body]
Yes that's exactly what I needed. Thanks a lot.
pozdrowienia
mk
2012/11/20 Alvaro Herrera <alvherre@2ndquadrant.com>
> Marcin Krawczyk escribi=F3:
> > 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')
>
> --
> =C1lvaro Herrera http://www.2ndQuadrant.com/
> PostgreSQL Development, 24x7 Support, Training & Services
>
[Attachment #3 (text/html)]
Yes that's exactly what I needed. Thanks a lot.<div class="gmail_extra"><br \
clear="all">pozdrowienia<br>mk<br> <br><br><div class="gmail_quote">2012/11/20 Alvaro \
Herrera <span dir="ltr"><<a href="mailto:alvherre@2ndquadrant.com" \
target="_blank">alvherre@2ndquadrant.com</a>></span><br><blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex">
Marcin Krawczyk escribió:<br>
<div><div class="h5">> Hi list,<br>
><br>
> I'm trying to use regexp_replace to get rid of all occurrences of<br>
> certain sub strings from my string.<br>
> What I'm doing is:<br>
><br>
> SELECT regexp_replace('F0301 305-149-101-0 F0302 {x1} 12W47 0635H<br>
> {tt}{POL23423423}', E'\{.+\}', '', 'g')<br>
><br>
> so get rid of whatever is between { } along with these,<br>
><br>
> but it results in:<br>
> 'F0301 305-149-101-0 F0302 '<br>
><br>
> how do I get it to be:<br>
> 'F0301 305-149-101-0 F0302 12W47 0635H'<br>
><br>
> ??<br>
><br>
> as I understood the docs, the g flag "specifies replacement of each<br>
> matching substring rather than only the first one"<br>
<br>
</div></div>The first \{.+\} match starts at the first { and ends at the last },<br>
eating the {s and }s in the middle. So there's only one match and that's<br>
what's removed.<br>
<div class="im"><br>
> what am I missing ?<br>
<br>
</div>You need a non-greedy quantifier. Try<br>
<br>
SELECT regexp_replace('F0301 305-149-101-0 F0302 {x1} 12W47 0635H \
{tt}{POL23423423}', E'\{.+?\}', '', 'g')<br> <span \
class="HOEnZb"><font color="#888888"><br>
--<br>
Álvaro Herrera <a href="http://www.2ndQuadrant.com/" \
target="_blank">http://www.2ndQuadrant.com/</a><br> PostgreSQL Development, 24x7 \
Support, Training & Services<br> </font></span></blockquote></div><br></div>
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic