Re: Problem using $1 in substitution command



Andreas Karlsson wrote:
Hi guys,

Hello,

I'm doing a script to search through a lot of files and doing some replacements.
It is a lot of different replacements that has to be done so I list them in a .txt
instruction file in two columns representing the strings to be matched and replaced
and the new text.
A problem arise when doing some more complex substitution, wanting to keep certain
parts of a string while replacing others. To be specific in this case I want to
remove the '' surrounding a string. The string between '' will change.

The problem comes from that I want to use the \1 (or $1) variable but since the
substitution command looks like
$line=~ s/$subst_from/$subst_to/ig;

and the $1 is incorporated into the $subst_to variable it is being populated when
the replecement string is assinged to $subst_to.
How can I keep the substitution command as general as I have it, always taking the
same to variables but still making use of the $1,...,$9 feature of Perl?
I post you a test-program which shows my problem.
List.\1 being printed after 'Result:' is the problem.

It's developed in an cygwin-environment and executes using the command
perl PerlSubstituteProblem.pl

The printouts looks like this when I run it:

Script started.
subst_to: List.\1
'' should be removed in List.'123_KeepThis_123'
$1_1: 123_KeepThis_123
$1_2: 123_KeepThis_123
Result:
'' should be removed in List.\1
Same thing but non-general s//:
'' should be removed in List.123_KeepThis_123
Changed in 1 places


________Program_______________
#!/usr/local/bin/perl
use strict;
use strict 'refs';
$|=1; #Flush ON
print "Script started.\n";

my($nr_of_changes,$line,$line_copy, $subst_from,$subst_to)=0;


$subst_from="List\\.'(\\w*)'";
$subst_to="List.\\1";
print "subst_to: $subst_to \n";

$line="'' should be removed in List.'123_KeepThis_123' \n";
print "$line";

if($line =~/$subst_from/i)
{
$line_copy=$line;
print "\$1_1: $1 \n";
$nr_of_changes++;
$line=~ s/$subst_from/$subst_to/ig;
print "\$1_2: $1 \n";
print "Result: \n";
print "$line";

print "Same thing but non-general s//: \n";
$line_copy=~ s/$subst_from/List.\1/ig;
print "$line_copy";

}

print"Changed in $nr_of_changes places\n";

exit(0);



$ perl -le'
my $subst_from = qr/(?<=List\.)\047(\w*)\047/;
my $subst_to = q/$1/;

my $line = qq/\047\047 should be removed in List.\047123_KeepThis_123\047 \n/;

print for $subst_from, $subst_to, $line;

my $nr_of_changes = $line =~ s/$subst_from/$subst_to/eeig;

print for $nr_of_changes, $line;
'
(?-xism:(?<=List\.)\047(\w*)\047)
$1
'' should be removed in List.'123_KeepThis_123'

1
'' should be removed in List.123_KeepThis_123




John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
.



Relevant Pages

  • Problem using $1 in substitution command
    ... I'm doing a script to search through a lot of files and doing some replacements. ... parts of a string while replacing others. ... How can I keep the substitution command as general as I have it, ...
    (perl.beginners)
  • Re: regexp for matching a string with mandatory underscores
    ... character with a '_' character and returns the number of ... replacements made. ... Not when counting or replacing character in a non-UTF8 string. ... Particularly with Perl: systems I'm on have ...
    (comp.lang.perl.misc)
  • Cant Compiling perl5.8.8 on FreeBSD6.2
    ... WITH_DEBUGGING=yes Build perl with debugging support. ... First let's make sure your kit is complete. ... What is the file extension used for shared libraries? ... I'll use sprintf to convert floats into a string. ...
    (comp.unix.bsd.freebsd.misc)
  • Re: Replace Count
    ... If you want to know the number of replacements required then it would be faster to run through the string first using InString in a loop and modifying the Start parameter within the loop to each time point to the first character after the previously found substring. ... Firstly, in your code you are replacing multiple vbCr characters whereas most standard Windows .txt files use the character pair vbCrLf instead of just a vbCr character, so presumably you are working with a text file that does use just vbCr to signify a new line, of which of course there are many, but you do need to know which kind of text file you are working with. ... Dim OldPointer As Long, NewPointer As Long ...
    (microsoft.public.vb.general.discussion)
  • Re: use of DBI; I am getting multiple error messages mixed in with ?the correct output.
    ... But I'm not talking about C++ or Java or SQL. ... This is a perl newsgroup. ... to means by a "defined null string", as opposed to an undefined value. ... And where exactly are you getting the idea that the empty string is a ...
    (comp.lang.perl.misc)