Re: RegExp Replace Using a Variable

From: jan (jan_at_ossifrage.net)
Date: 02/26/04

  • Next message: Gunnar Hjalmarsson: "Re: RegExp Replace Using a Variable"
    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


  • Next message: Gunnar Hjalmarsson: "Re: RegExp Replace Using a Variable"

    Relevant Pages

    • Re: RegExp Replace Using a Variable
      ... 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. ... I would generate a little program from perl, ... executable program to perform the actual work. ...
      (comp.lang.perl)
    • Re: Who said registers are born equal ?
      ... Use the correct tool for the correct job. ... I am not at the level of perl to do this yet, however, i don't even have ... twist RosAsm to change an entire C header file into one source code of ... (perl and other regex commands) you speed up ...
      (alt.lang.asm)
    • Help required
      ... I am coding a program in Perl which invokes another executable program ...
      (comp.lang.perl.misc)
    • Re: how do i describe a perl user?
      ... i say to be a guru someone has to be nutcrack... ... I read somewhere Perl monks are like maniacs or something.. ...
      (perl.beginners)
    • Re: Help me choose a programming language please
      ... Perl is the ideal language for all of your needs. ... not met easily by Perl is the abiltity to create a completely portable, ... executable program with a graphic user interface. ...
      (comp.programming)