Re: Regular Expression Help?

From: elein <elein(at)varlena(dot)com>
To: David Fetter <david(at)fetter(dot)org>
Cc: SF Postgres <sfpug(at)postgresql(dot)org>
Subject: Re: Regular Expression Help?
Date: 2003-11-05 00:53:29
Message-ID: 20031104165329.R10455@cookie.varlena.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: sfpug

I'm sure Wheeler can do this in one line... But

Throw it in a while loop to get all iterations.
This assumes that the values are in value[ "#DETAIL" ], etc
and $line is the line you are parsing.

# substitute variables
while( $line =~ /(#[A-Z]+)[\W ]+/ ) {
$column = $1;
$val = replacements[ $column ];
$line =~ s/$column/$val/;
}

--elein

On Tue, Nov 04, 2003 at 04:47:59PM -0800, David Fetter wrote:
> 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

Browse sfpug by date

  From Date Subject
Next Message David Wheeler 2003-11-05 02:58:49 Re: Regular Expression Help?
Previous Message David Fetter 2003-11-05 00:47:59 Re: Regular Expression Help?