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";

In reply to Stupid question about regex with string followed variable in replacement by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.