Re: REGEXP_REPLACE woes

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: "Leif B(dot) Kristensen" <leif(at)solumslekt(dot)org>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: REGEXP_REPLACE woes
Date: 2008-06-10 14:07:07
Message-ID: 20080610140706.GA52725@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Jun 10, 2008 at 07:41:53AM -0600, Michael Fuhr wrote:
> On Tue, Jun 10, 2008 at 02:59:53PM +0200, Leif B. Kristensen wrote:
> > So far, so good. But look here:
> >
> > pgslekt=> select link_expand('[p=123|John Smith] and [p=456|Jane Doe]');
> > link_expand
> > -----------------------------------------------------------------------
> > <a href="./family.php?person=123">John Smith] and [p=456|Jane Doe</a>
> > (1 row)
> >
> > Hey, I told it not to be greedy, didn't I?
>
> Yes, but regexp_replace only replaces that part of the original
> string that matches the regular expression -- the rest it leaves
> alone.

Sorry, this isn't quite right. As you already discovered, the
pattern was being more greedy than you wanted. That's one reason
why I often use an inverted class instead of assuming that a
non-greedy quantifier will grab only what I want.

select regexp_replace(
'[p=123|John Smith] and [p=456|Jane Doe]',
E'\\[p=(\\d+)\\|([^]]+)\\]',
E'<a href="./family.php?person=\\1">\\2</a>',
'g'
);

regexp_replace
-----------------------------------------------------------------------------------------------------
<a href="./family.php?person=123">John Smith</a> and <a href="./family.php?person=456">Jane Doe</a>

--
Michael Fuhr

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Leif B. Kristensen 2008-06-10 14:10:58 Re: REGEXP_REPLACE woes
Previous Message CaT 2008-06-10 14:02:44 Re: REGEXP_REPLACE woes