In Perl, we use $1 instead of \1 in the substitution string. The right hand side is just another interpolated string.

From perlop:

It is at this step that "\1" is begrudgingly converted to $1 in the replacement text of "s///" to correct the incorrigible sed hackers who haven't picked up the saner idiom yet. A warning is emitted if the "use warnings" pragma or the -w command-line flag (that is, the $^W variable) was set.

From Jeffrey E. F. Friedl's great book Mastering Regular Expressions:

The Perl manpage makes a concerted effort to point out that \1 is not available as a backreerence outside of a regex. (Use the variable $1 instead.) \1 is much more than a simple notational convenience - the variable $1 refers to a string of static text matched during some previously completed successful match. On the other hand, \1 is a true regex metacharacter to match text similar to that matched within the first parenthesized subexpression (...)

perl -we's//\1/' 2>&1 |splain

\1 better written as $1 at -e line 1 (#1)
(W syntax) Outside of patterns, backreferences live on as variables. The use of backslashes is grandfathered on the right-hand side of a substitution, but stylistically it's better to use the variable form because other Perl programmers will expect it, and it works better if there are more than 9 backreferences.

And dot star (.*) is bad in this case.

2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$


In reply to Re: Re: Regex that will append to end of same line by Juerd
in thread Regex that will append to end of same line by data67

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.