RegExp Replace Using a Variable

Discussion in 'Perl' started by Ones Self, Feb 24, 2004.

  1. Ones Self

    Ones Self Guest

    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.
     
    Ones Self, Feb 24, 2004
    #1
    1. Advertising

  2. 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_____
     
    ___cliff rayman___, Feb 25, 2004
    #2
    1. Advertising

  3. Ones Self

    Ones Self Guest

    ___cliff rayman___ <> wrote in message news:<78T_b.1850$>...
    > 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
     
    Ones Self, Feb 25, 2004
    #3
  4. Ones Self wrote:

    >___cliff rayman___ <> wrote in message news:<78T_b.1850$>...
    >
    >
    >>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_____
     
    ___cliff rayman___, Feb 26, 2004
    #4
  5. Ones Self

    jan Guest

    ___cliff rayman___ <> wrote in message news:<GMg%b.2670$>...
    > Ones Self wrote:
    > >___cliff rayman___ <> wrote in message news:<78T_b.1850$>...
    > >>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
     
    jan, Feb 26, 2004
    #5
  6. 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
     
    Gunnar Hjalmarsson, Feb 26, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Pye
    Replies:
    8
    Views:
    448
    John Pye
    Mar 22, 2007
  2. Alun
    Replies:
    3
    Views:
    4,663
    Masudur
    Feb 18, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    409
    7stud --
    Aug 21, 2009
  4. Ones Self

    RegExp Replace Using a Variable

    Ones Self, Feb 25, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    108
    David K. Wall
    Feb 27, 2004
  5. X l e c t r i c
    Replies:
    2
    Views:
    113
    X l e c t r i c
    Mar 2, 2008
Loading...

Share This Page