Re: RegExp Replace Using a Variable
From: jan (jan_at_ossifrage.net)
Date: 02/26/04
- Previous message: Gunnar Hjalmarsson: "Re: assigning back to an array"
- In reply to: ___cliff rayman___: "Re: RegExp Replace Using a Variable"
- Next in thread: Gunnar Hjalmarsson: "Re: RegExp Replace Using a Variable"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 26 Feb 2004 13:54:44 -0800
___cliff rayman___ <cliff@rayman.com> wrote in message news:<GMg%b.2670$Bb5.1740@twister.socal.rr.com>...
> Ones Self wrote:
> >___cliff rayman___ <cliff@rayman.com> wrote in message news:<78T_b.1850$wP3.682@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
- Previous message: Gunnar Hjalmarsson: "Re: assigning back to an array"
- In reply to: ___cliff rayman___: "Re: RegExp Replace Using a Variable"
- Next in thread: Gunnar Hjalmarsson: "Re: RegExp Replace Using a Variable"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|