Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   RegExp Replace Using a Variable (http://www.velocityreviews.com/forums/t885440-regexp-replace-using-a-variable.html)

Ones Self 02-25-2004 02:40 PM

RegExp Replace Using a Variable
 
Hi all:

I'm trying to replace using a regexp read from a file:

$string = '123 456 789';
# these two are usualy read from a file,
# and so have to be in variables.
$re = '([258])';
$rep = '|$1|';

$string =~ s/$re/$rep/g;

I would like $string to be: 1|2|3 4|5|6 7|8|9
but it is: 1|$1|3 4|$1|6 7|$1|9

I understand why this is happening, but how do I make
it do what I want?

I've been reading around, I found an answer that almost works:
$string = '123 456 789';
# these two are usualy read from a file
$re = '([258])';
$rep = sub { "|$1|" };

$string =~ s/$re/$rep->()/ge;

This works, but it's not what I want. I want:
$string = '123 456 789';
# these two are usualy read from a file
$re = '([258])';
$repVar = '|$1|' # or "|$1|" doesn't work either way
$rep = sub { $repVar };

$string =~ s/$re/$rep->()/ge;

But this doesn't work.

Any help would be appritieted.
Thanks.

Ben Morrow 02-25-2004 03:10 PM

Re: RegExp Replace Using a Variable
 

nutgg001@sneakemail.com (Ones Self) wrote:
> I'm trying to replace using a regexp read from a file:
>
> $string = '123 456 789';
> # these two are usualy read from a file,
> # and so have to be in variables.
> $re = '([258])';
> $rep = '|$1|';
>
> $string =~ s/$re/$rep/g;
>
> I would like $string to be: 1|2|3 4|5|6 7|8|9
> but it is: 1|$1|3 4|$1|6 7|$1|9


We've just had this thread...

You need to double-qq-quote it, and use /ee:

$string =~ s/$re/qq!"$rep"!/gee;

Note that this is a string eval, so you are open to the possibility of
arbitrary code from the file: make sure you trust its contents.

Ben

--
Razors pain you / Rivers are damp
Acids stain you / And drugs cause cramp. [Dorothy Parker]
Guns aren't lawful / Nooses give
Gas smells awful / You might as well live. ben@morrow.me.uk

Brian McCauley 02-25-2004 06:00 PM

Re: RegExp Replace Using a Variable
 
Ben Morrow <usenet@morrow.me.uk> writes:

> nutgg001@sneakemail.com (Ones Self) wrote:
> > I'm trying to replace using a regexp read from a file:
> >
> > $string = '123 456 789';
> > # these two are usualy read from a file,
> > # and so have to be in variables.
> > $re = '([258])';
> > $rep = '|$1|';
> >
> > $string =~ s/$re/$rep/g;
> >
> > I would like $string to be: 1|2|3 4|5|6 7|8|9
> > but it is: 1|$1|3 4|$1|6 7|$1|9

>
> We've just had this thread...


And I commented there that this question comes in waves. It goes
unaked for weeks then along come two in two days.

> You need to double-qq-quote it, and use /ee:
>
> $string =~ s/$re/qq!"$rep"!/gee;


As I belive I implied yesterday's thread, you really shouldn't present
the qq solution without also showing the here-doc one:

$string =~ s/$re/chop (my $r = eval "<<_END_\n$rep\n_END_"); $r/ge;

Sure it's not as pretty-looking as the qq solution but unlike the qq
solution there is no single character in $rep that needs to be
avoided. Sure you have to avoid the sequence "\n_END_" but that's
rather less likely to crop up by accident.

As I also pointed out yesterday it is quite likely that it really
makes sense put the whole Perl statement in the config file rather
than a pair of Perl regex and a Perl string interpolation.

> Note that this is a string eval, so you are open to the possibility of
> arbitrary code from the file: make sure you trust its contents.


And (as I think I forgot to say yesterday) perhaps this is XY and if
you really do trust the file to execute arbitrary code you simply
should turn the whole problem inside out.

http://groups.google.com/groups?thre...ing.google.com


--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\

David K. Wall 02-26-2004 01:21 AM

Re: RegExp Replace Using a Variable
 
Brian McCauley <nobull@mail.com> wrote:

> Ben Morrow <usenet@morrow.me.uk> writes:
>
>> We've just had this thread...

>
> And I commented there that this question comes in waves. It goes
> una[s]ked for weeks then along come two in two days.


Maybe it follows a Poisson distribution.....

If I could figure out how to find all occurences with Google, it might be an
interesting thing to look at. Not that this has anything to do with Perl.
:-)


gnari 02-26-2004 10:28 AM

Re: RegExp Replace Using a Variable
 
"David K. Wall" <dwall@fastmail.fm> wrote in message
news:Xns949ACF2798508dkwwashere@216.168.3.30...
> Brian McCauley <nobull@mail.com> wrote:
>
> > Ben Morrow <usenet@morrow.me.uk> writes:
> >
> >> We've just had this thread...

> >
> > And I commented there that this question comes in waves. It goes
> > una[s]ked for weeks then along come two in two days.

>
> Maybe it follows a Poisson distribution.....


is there an astrologer in this group ? :-)

gnari





nobull@mail.com 02-27-2004 06:20 PM

Re: RegExp Replace Using a Variable
 
"David K. Wall" <dwall@fastmail.fm> wrote in message news:<Xns949ACF2798508dkwwashere@216.168.3.30>...
> Brian McCauley <nobull@mail.com> wrote:
>
> > Ben Morrow <usenet@morrow.me.uk> writes:
> >
> >> We've just had this thread...

> >
> > And I commented there that this question comes in waves. It goes
> > una[s]ked for weeks then along come two in two days.

>
> Maybe it follows a Poisson distribution.....
>
> If I could figure out how to find all occurences with Google, it might be an
> interesting thing to look at.


I was planning to do a lightning talk at YAPC::Europe::2004 on this
question.

So if you can get off your little Pacific island[1] and get to
Northern Ireland you may just get to look at this interesting think.

[1] Or are you flying a flag of convenience?

David K. Wall 02-27-2004 07:49 PM

Re: RegExp Replace Using a Variable
 
<nobull@mail.com> wrote:

> "David K. Wall" <dwall@fastmail.fm> wrote in message
> news:<Xns949ACF2798508dkwwashere@216.168.3.30>...
>> Brian McCauley <nobull@mail.com> wrote:
>>
>> > Ben Morrow <usenet@morrow.me.uk> writes:
>> >
>> >> We've just had this thread...
>> >
>> > And I commented there that this question comes in waves. It goes
>> > una[s]ked for weeks then along come two in two days.

>>
>> Maybe it follows a Poisson distribution.....
>>
>> If I could figure out how to find all occurences with Google, it might
>> be an interesting thing to look at.

>
> I was planning to do a lightning talk at YAPC::Europe::2004 on this
> question.


Cool! Are you going to put it on the web after the conference?

> So if you can get off your little Pacific island[1] and get to
> Northern Ireland you may just get to look at this interesting think.


I'd like to, but I don't think my employer would pay for it, and I'm
unwilling to pay my own way.

> [1] Or are you flying a flag of convenience?


Convenience. fastmail.fm is just where I get my email, like you and (I
suppose) mail.com. I'm actually in Cincinnati, not Micronesia. :-)

--
David "but there's no WKRP here" Wall


All times are GMT. The time now is 07:46 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.