RegExp Replace Using a Variable

Discussion in 'Perl Misc' started by Ones Self, Feb 25, 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 25, 2004
    #1
    1. Advertising

  2. Ones Self

    Ben Morrow Guest

    (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


    We've just had this thread...

    You need to double-qq-quote it, and use /ee:

    $string =~ s/$re/qq!"$rep"!/gee;

    Note that this is a string eval, so you are open to the possibility of
    arbitrary code from the file: make sure you trust its contents.

    Ben

    --
    Razors pain you / Rivers are damp
    Acids stain you / And drugs cause cramp. [Dorothy Parker]
    Guns aren't lawful / Nooses give
    Gas smells awful / You might as well live.
     
    Ben Morrow, Feb 25, 2004
    #2
    1. Advertising

  3. Ben Morrow <> writes:

    > (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

    >
    > We've just had this thread...


    And I commented there that this question comes in waves. It goes
    unaked for weeks then along come two in two days.

    > You need to double-qq-quote it, and use /ee:
    >
    > $string =~ s/$re/qq!"$rep"!/gee;


    As I belive I implied yesterday's thread, you really shouldn't present
    the qq solution without also showing the here-doc one:

    $string =~ s/$re/chop (my $r = eval "<<_END_\n$rep\n_END_"); $r/ge;

    Sure it's not as pretty-looking as the qq solution but unlike the qq
    solution there is no single character in $rep that needs to be
    avoided. Sure you have to avoid the sequence "\n_END_" but that's
    rather less likely to crop up by accident.

    As I also pointed out yesterday it is quite likely that it really
    makes sense put the whole Perl statement in the config file rather
    than a pair of Perl regex and a Perl string interpolation.

    > Note that this is a string eval, so you are open to the possibility of
    > arbitrary code from the file: make sure you trust its contents.


    And (as I think I forgot to say yesterday) perhaps this is XY and if
    you really do trust the file to execute arbitrary code you simply
    should turn the whole problem inside out.

    http://groups.google.com/groups?threadm=


    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
     
    Brian McCauley, Feb 25, 2004
    #3
  4. Brian McCauley <> wrote:

    > Ben Morrow <> writes:
    >
    >> We've just had this thread...

    >
    > And I commented there that this question comes in waves. It goes
    > unaked for weeks then along come two in two days.


    Maybe it follows a Poisson distribution.....

    If I could figure out how to find all occurences with Google, it might be an
    interesting thing to look at. Not that this has anything to do with Perl.
    :)
     
    David K. Wall, Feb 26, 2004
    #4
  5. Ones Self

    gnari Guest

    "David K. Wall" <> wrote in message
    news:Xns949ACF2798508dkwwashere@216.168.3.30...
    > Brian McCauley <> wrote:
    >
    > > Ben Morrow <> writes:
    > >
    > >> We've just had this thread...

    > >
    > > And I commented there that this question comes in waves. It goes
    > > unaked for weeks then along come two in two days.

    >
    > Maybe it follows a Poisson distribution.....


    is there an astrologer in this group ? :)

    gnari
     
    gnari, Feb 26, 2004
    #5
  6. Ones Self

    Guest

    "David K. Wall" <> wrote in message news:<Xns949ACF2798508dkwwashere@216.168.3.30>...
    > Brian McCauley <> wrote:
    >
    > > Ben Morrow <> writes:
    > >
    > >> We've just had this thread...

    > >
    > > And I commented there that this question comes in waves. It goes
    > > unaked for weeks then along come two in two days.

    >
    > Maybe it follows a Poisson distribution.....
    >
    > If I could figure out how to find all occurences with Google, it might be an
    > interesting thing to look at.


    I was planning to do a lightning talk at YAPC::Europe::2004 on this
    question.

    So if you can get off your little Pacific island[1] and get to
    Northern Ireland you may just get to look at this interesting think.

    [1] Or are you flying a flag of convenience?
     
    , Feb 27, 2004
    #6
  7. <> wrote:

    > "David K. Wall" <> wrote in message
    > news:<Xns949ACF2798508dkwwashere@216.168.3.30>...
    >> Brian McCauley <> wrote:
    >>
    >> > Ben Morrow <> writes:
    >> >
    >> >> We've just had this thread...
    >> >
    >> > And I commented there that this question comes in waves. It goes
    >> > unaked for weeks then along come two in two days.

    >>
    >> Maybe it follows a Poisson distribution.....
    >>
    >> If I could figure out how to find all occurences with Google, it might
    >> be an interesting thing to look at.

    >
    > I was planning to do a lightning talk at YAPC::Europe::2004 on this
    > question.


    Cool! Are you going to put it on the web after the conference?

    > So if you can get off your little Pacific island[1] and get to
    > Northern Ireland you may just get to look at this interesting think.


    I'd like to, but I don't think my employer would pay for it, and I'm
    unwilling to pay my own way.

    > [1] Or are you flying a flag of convenience?


    Convenience. fastmail.fm is just where I get my email, like you and (I
    suppose) mail.com. I'm actually in Cincinnati, not Micronesia. :)

    --
    David "but there's no WKRP here" Wall
     
    David K. Wall, Feb 27, 2004
    #7
    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. Ones Self

    RegExp Replace Using a Variable

    Ones Self, Feb 24, 2004, in forum: Perl
    Replies:
    5
    Views:
    590
    Gunnar Hjalmarsson
    Feb 26, 2004
  2. John Pye
    Replies:
    8
    Views:
    448
    John Pye
    Mar 22, 2007
  3. Alun
    Replies:
    3
    Views:
    4,663
    Masudur
    Feb 18, 2008
  4. Joao Silva
    Replies:
    16
    Views:
    409
    7stud --
    Aug 21, 2009
  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