[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&#39;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">&lt;<a href="mailto:alvherre@2ndquadrant.com" \
target="_blank">alvherre@2ndquadrant.com</a>&gt;</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">&gt; Hi list,<br>
&gt;<br>
&gt; I&#39;m trying to use regexp_replace to get rid of all occurrences of<br>
&gt; certain sub strings from my string.<br>
&gt; What I&#39;m doing is:<br>
&gt;<br>
&gt; SELECT regexp_replace(&#39;F0301 305-149-101-0 F0302 {x1} 12W47 0635H<br>
&gt; {tt}{POL23423423}&#39;, E&#39;\{.+\}&#39;, &#39;&#39;, &#39;g&#39;)<br>
&gt;<br>
&gt; so get rid of whatever is between { } along with these,<br>
&gt;<br>
&gt; but it results in:<br>
&gt; &#39;F0301 305-149-101-0 F0302 &#39;<br>
&gt;<br>
&gt; how do I get it to be:<br>
&gt; &#39;F0301 305-149-101-0 F0302 12W47 0635H&#39;<br>
&gt;<br>
&gt; ??<br>
&gt;<br>
&gt; as I understood the docs, the g flag &quot;specifies replacement of each<br>
&gt; matching substring rather than only the first one&quot;<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&#39;s only one match and that&#39;s<br>
what&#39;s removed.<br>
<div class="im"><br>
&gt; what am I missing ?<br>
<br>
</div>You need a non-greedy quantifier.  Try<br>
<br>
 SELECT regexp_replace(&#39;F0301 305-149-101-0 F0302 {x1} 12W47 0635H \
{tt}{POL23423423}&#39;, E&#39;\{.+?\}&#39;, &#39;&#39;, &#39;g&#39;)<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 &amp; 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