Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks. I need a little help.
Sorry for posting this, but I can't find solution by myself. I have very stupid question about regexp with string followed variable in replacement.
For example: $1 and 0 (zero) after it. $foo with bar after it.
It's easy to avoid it, by assigning 0 and bar to variable or by placing some special character, that not usable as part of variable name, for example slash.
But is there any way to do that another way? Maybe there is some character, that could be used as separator?
N.B. Examples below completely pointless and stupid, they just to illustrate the problem.
Example of problem #1 (of course it's not working, since we don't have $foobar variable, it should be $foo variable and bar word after it):
#!/usr/bin/perl use warnings; use strict; my $txt = 'test'; my $foo = 'foo'; $txt =~ s/test/$foobar/; print "$txt\n";
"Fix" for example problem #1:
#!/usr/bin/perl use warnings; use strict; my $txt = 'test'; my $foo = 'foo'; my $fix = 'bar'; $txt =~ s/test/$foo$fix/; print "$txt\n";
Example of problem #2 (of course it's not working, since we don't have $10, it should be $1 variable and 0 (zero) after it):
#!/usr/bin/perl use warnings; use strict; my $txt = '1,2'; $txt =~ s/(\d+)\.(\d+)/$10,$2/;
"Fix" for example problem #2:
#!/usr/bin/perl use warnings; use strict; my $txt = '1,2'; my $fix = 0; $txt =~ s/(\d+)\.(\d+)/$1$fix,$2/; print "$txt\n";
|
|---|