RegExp Replace Using a Variable

O

Ones Self

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.
 
C

___cliff rayman___

Ones said:
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;
 
O

Ones Self

___cliff rayman___ said:
Ones said:
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
 
C

___cliff rayman___

Ones said:
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.
 
J

jan

___cliff rayman___ said:
Ones said:
___cliff rayman___ said:
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
 
G

Gunnar Hjalmarsson

Ones said:
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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top