469,273 Members | 1,620 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,273 developers. It's quick & easy.

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.
Jul 19 '05 #1
5 13910

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

$string = '123 456 789';
$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

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

$doit="\$string=~s/$re/$rep/g";
eval "$doit";

print $string;

--
_____cliff_rayman_____cliff_@_rayman_._com_____

Jul 19 '05 #2
___cliff rayman___ <cl***@rayman.com> wrote in message news:<78****************@twister.socal.rr.com>...
Ones Self wrote:
I'm trying to replace using a regexp read from a file:

$string = '123 456 789';
$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

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

$doit="\$string=~s/$re/$rep/g";
eval "$doit";

print $string;


I appologize for neglecting to mention this in my original post,
but this is actually what I'm currently using. However, since
I'm dealing with a _lot_ of records (~1.5M lines), eval is
really slow (about 8 times slower than a static regexp).
Furthermore, if there was a way to compile the regexp
that would be very nice.

Thanks
Jul 19 '05 #3

Ones Self wrote:
___cliff rayman___ <cl***@rayman.com> wrote in message news:<78****************@twister.socal.rr.com>...

Ones Self wrote:

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

$string = '123 456 789';
$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


$string = '123 456 789';
$re = '([258])';
$rep = '|$1|';

$doit="\$string=~s/$re/$rep/g";
eval "$doit";

print $string;

I'm dealing with a _lot_ of records (~1.5M lines), eval is
really slow (about 8 times slower than a static regexp).

OK - I am not sure how to get around this problem using regex commands.
Maybe a guru would know of one of the top of their head. If it were me,
I would generate a little program from perl, then execute it. It would
use the regex's from the file, and a template to generate a perl
executable program to perform the actual work.

--
_____cliff_rayman_____cliff_@_rayman_._com_____

Jul 19 '05 #4
jan
___cliff rayman___ <cl***@rayman.com> wrote in message news:<GM*****************@twister.socal.rr.com>...
Ones Self wrote:
___cliff rayman___ <cl***@rayman.com> wrote in message news:<78****************@twister.socal.rr.com>...
Ones Self wrote:

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

$string = '123 456 789';
$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
$string = '123 456 789';
$re = '([258])';
$rep = '|$1|';

$doit="\$string=~s/$re/$rep/g";
eval "$doit";

print $string;

I'm dealing with a _lot_ of records (~1.5M lines), eval is
really slow (about 8 times slower than a static regexp).

OK - I am not sure how to get around this problem using regex commands.
Maybe a guru would know of one of the top of their head. If it were me,
I would generate a little program from perl, then execute it. It would
use the regex's from the file, and a template to generate a perl
executable program to perform the actual work.


Your problem is that $1 has no value at the time that you are
assigning it's contents to $rep. This should do what you want:

$string = "123 456 789";
$re = "([258])";
$string =~ /$re/g;
$foo = $1;
$rep = "|".$foo."|";
$string =~ s/$re/$rep/og;

print $string, "\n";
Cheers,
Jan
Jul 19 '05 #5
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


You could for instance create and use a hash:

my $string = '123 456 789';
my $re = qr/([258])/; # compiles $re
my $rep = '|$1|';

my @rep = $rep =~ /(.*)\$1(.*)/;
my %rep;
$rep{$1} = "$rep[0]$1$rep[1]" while $re =~ /(\d)/g;

$string =~ s/$re/$rep{$1}/g;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Ian Renfrew | last post: by
1 post views Thread by eysteinbye | last post: by
13 posts views Thread by Phat G5 (G3) | last post: by
7 posts views Thread by Csaba Gabor | last post: by
8 posts views Thread by reflex | last post: by
3 posts views Thread by yoni | last post: by
2 posts views Thread by X l e c t r i c | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.