Professional Web Applications Themes

Regex:: using variables to hold replacement text - PERL Beginners

Hi, I have a need to store replacement text for a regex substitution in a variable. In other words, I have code in a perl script like so:: $foo =~ s/(.*)bar(.*)/$replacement/g; The $replacement string needs to contain a "reference" to $1. In other words, the string $replacement will contain the string "$1". I need to have the $1 string interpreted so that it will be replaced with the text of the first '(.*)' expression. I have not yet found a way to get this to happen. The "$1" string always seems to be interpreted as a plain string. Can someone ...

  1. #1

    Default Regex:: using variables to hold replacement text

    Hi,
    I have a need to store replacement text for a regex substitution
    in a variable. In other words, I have code in a perl script like so::

    $foo =~ s/(.*)bar(.*)/$replacement/g;

    The $replacement string needs to contain a "reference" to $1. In other
    words, the string $replacement will contain the string "$1". I need to
    have the $1 string interpreted so that it will be replaced with the text
    of the first '(.*)' expression. I have not yet found a way to get this
    to happen. The "$1" string always seems to be interpreted as a plain
    string.

    Can someone tell me how to write the substitution correctly? I
    have included an example below.

    Thanks for your help and your time.


    Bill Brown
    com

    ++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++
     

    --
    William L. Brown
    Email: com


    William Guest

  2. #2

    Default Re: Regex:: using variables to hold replacement text

    William Lewis Brown wrote: 

    You are asking a FAQ.

    perldoc -q "expand variables"

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Guest

  3. #3

    Default RE: Regex:: using variables to hold replacement text

    William Lewis Brown wrote: 

    You need to use eval and /e

    $foo =~ s/(.*)bar(.*)/eval $replacement/ge;
    Bob Guest

  4. #4

    Default RE: Regex:: using variables to hold replacement text

    On Thu, 2004-09-30 at 10:58, Bob Showalter wrote: 
    >
    > You need to use eval and /e
    >
    > $foo =~ s/(.*)bar(.*)/eval $replacement/ge;[/ref]

    Hi,
    That definitely did the trick. I had tried "eval" and "/e" but never
    in conjunction. Thanks for your time and your help.

    Bill

    --
    William L. Brown
    Email: com

    William Guest

  5. #5

    Default RE: Regex:: using variables to hold replacement text

    Hi Bob,
    I tried what you suggested and I keep getting a unitialized value error.
    here is my code. If you could point me to how I could do what William is
    trying to do.

    Thanks
    Radhika

    $_= "goodbarrad";
    #$foo =~ s/(.*)bar(.*)/$replacement/g;
    my $foo =~ s/(.*)bar(.*)/eval $1/ge;
    print "\$1 is: $1\n";
    print "$foo\n";


    -------------------------------------------------- 
    >>
    >> You need to use eval and /e
    >>
    >> $foo =~ s/(.*)bar(.*)/eval $replacement/ge;[/ref]
    >
    > Hi,
    > That definitely did the trick. I had tried "eval" and "/e" but never
    > in conjunction. Thanks for your time and your help.
    >
    > Bill
    >
    > --
    > William L. Brown
    > Email: com
    >
    >
    > --
    > To unsubscribe, e-mail: org
    > For additional commands, e-mail: org
    > <http://learn.perl.org/> <http://learn.perl.org/first-response>
    >
    >
    >[/ref]


    --
    It's all a matter of perspective. You can choose your view by choosing
    where to stand.
    Larry Wall
    ---
    Radhika Guest

  6. #6

    Default RE: Regex:: using variables to hold replacement text

    Radhika wrote: 

    Sorry, I don't know what you're trying to do. Let's start over from the
    beginning. What are you trying to do?
    Bob Guest

Similar Threads

  1. Using variables in a regex
    By Kevin in forum PERL Beginners
    Replies: 4
    Last Post: May 7th, 05:32 PM
  2. text replacement+
    By Kiko Uehara in forum PERL Beginners
    Replies: 7
    Last Post: November 17th, 02:39 PM
  3. Using variables in RegEx statements?
    By Rodney in forum PERL Miscellaneous
    Replies: 11
    Last Post: September 24th, 02:13 PM
  4. text replacement in a memo filed
    By Allen Browne in forum Microsoft Access
    Replies: 0
    Last Post: June 30th, 04:02 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139