Skip site navigation (1) Skip section navigation (2)

Re: Regular Expression Help?

From: David Fetter <david(at)fetter(dot)org>
To: SF Postgres <sfpug(at)postgresql(dot)org>
Subject: Re: Regular Expression Help?
Date: 2003-11-05 00:47:59
Message-ID: 20031105004759.GR14711@fetter.org (view raw or flat)
Thread:
Lists: sfpug
On Tue, Nov 04, 2003 at 04:35:45PM -0800, Josh Berkus wrote:
> Perl geeks,
> 
> I'd like to write a regular expression for the following purpose:
> 
> In an incoming string, there are 0-1 instances of each of #NAME,
> #DATE, and #DETAIL.   I'd like to replace these labels in the string
> with the contents of their corresponding variable, $ename, $mdate,
> $edetail (which are coming from the database)

> I believe that this is possible to do in a single regular
> expression, but I'm not clear on how.  Currently, I'm doing 3
> substitutions, along the lines of:

> $eline =~ s/#NAME/$ename/g;
> 
> But that's not very sophisticated and I'd like to improve my use of
> regexp.  Suggestions?

IMHO, your current system may be easier to maintain than this:

%replacements = (
  '#NAME' => $ename
, '#DATE' => $mdate
, '#DETAIL' => $edetail
);

$eline =~ s/(#NAME|#DATE|#DETAIL)/$replacements{$1}/g;

Or it may not.  Anyhow, that's the way I'd set it up if I were
changing it :)

Cheers,
D
-- 
David Fetter david(at)fetter(dot)org http://fetter.org/
phone: +1 510 893 6100    cell: +1 415 235 3778

In response to

Responses

sfpug by date

Next:From: eleinDate: 2003-11-05 00:53:29
Subject: Re: Regular Expression Help?
Previous:From: Josh BerkusDate: 2003-11-05 00:35:45
Subject: Regular Expression Help?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group