Regex:: using variables to hold replacement text

From: William Lewis Brown (bbrown_at_addtoit.com)
Date: 09/30/04


To: beginners@perl.org
Date: Thu, 30 Sep 2004 09:48:08 -0400

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
                        bbrown@addtoit.com

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

> $ perl -d
>
> Loading DB routines from perl5db.pl version 1.0402
> Emacs support available.
>
> Enter h or `h h' for help.
>
> my $foo="yada bar yada";
> my $replacement='$1';
> $foo =~ s/(.*)bar(.*)/$replacement/g;
> print $foo;
> my $foo="yada bar yada";
> $foo =~ s/(.*)bar(.*)/$1/g;
> print $foo
> main::(-:1): my $foo="yada bar yada";
> DB<1> n
> main::(-:2): my $replacement='$1';
> DB<1> n
> main::(-:3): $foo =~ s/(.*)bar(.*)/$replacement/g;
> DB<1> n
> main::(-:4): print $foo;
> DB<2> n
> $1 [ BAD OUTPUT ]
> main::(-:5): my $foo="yada bar yada";
> DB<2> n
> main::(-:6): $foo =~ s/(.*)bar(.*)/$1/g;
> DB<2> n
> main::(-:7): print $foo
> DB<2> n
> yada [ GOOD OUTPUT]
> Debugged program terminated. Use q to quit or R to restart,
> use O inhibit_exit to avoid stopping after program termination,
> h q, h R or h O to get additional info.
> DB<2>

-- 
William L. Brown
Email: bbrown@addtoit.com


Relevant Pages

  • Re: Replace strings in a text file and get the number of replacementsmade
    ... It might be faster to use sb.Replace(search, replacement, i, search.Length) ... If you simply call StringBuilder.Replace(string, string, int, int) instead of having your own AreEqualmethod followed by a call to Removeand Insert, the performance should be practically identical, but you wouldn't get any information about how many replacements occurred. ... You could do a little hack by searching for the first character that differs between the search and replacement strings, and then bumping a counter after each call to StringBuilder.Replacebased on whether the character at the same offset within the current StringBuilder has changed. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Replace strings in a text file and get the number ofreplacementsmade
    ... I haven't tried, but I assume Regex would find 2 matches, while replace will replace just once the pattern. ... The regex will also be expensive, because it will have to be parsed/compiled every time a new pattern is used (and if it is a user defined replacement, that would be more often than not). ... I'd opt for an extention method on string and write an efficient version of a Replace method that returns the number of matches. ... If you simply call StringBuilder.Replace(string, string, int, int) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: replacing text in textfile
    ... ReplaceFile does indeed creat the replacement file - but nothing is in it. ... Public Function ReplaceInFile(InputFile As String, ... Dim Fnum As Integer ' get a FreeFile number ... Dim FileLength As Long 'Just in case its really big ...
    (microsoft.public.access.modulesdaovba)
  • Help in French|Spanish|German translation.
    ... I am also an author of User-defined string functions. ... WORDTRANEX (cSearched, cArExpressionSought | cExpressionSough, ... each string of the array is searched ... If the parameter nArStartOccurrence is -1 or omitted, the replacement starts ...
    (microsoft.public.fox.helpwanted)
  • Re: Regular expression optimization
    ... position in the replacement array of strings, ... > input string and a MatchEvaluator delegate. ... > The first part required combining the separate Regular Expression strings ...
    (microsoft.public.dotnet.general)