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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/