in reply to Substitution backreference woes

I'm trying to work out how Perl substitutions work. We're told that backreference patterns can be used in the substitution by specifying \1, \2 (etc), but it doesn't seem to work.

The docs don't just "tell" they give working examples of stuff

See perlvar#Variables related to regular expressions, perlrequick, Re: perldoc of s///ee wrong or just misleading? (read more)

Replies are listed 'Best First'.
Re^2: Substitution backreference woes
by Anonymous Monk on Jan 24, 2015 at 13:07 UTC

    Forgive me, but the working examples do not seem to cover the use of \1 (etc) in the replacement expression.

      Forgive me, but the working examples do not seem to cover the use of \1 (etc) in the replacement expression.
      It's covered in perlop
      Unlike sed, we use the \<digit> form in only the left hand side. Anywhere else it's $<digit>.
      Perl warns about it, too:
      $ perl -wE '$_ = "foo"; s/(foo)/bar \1/; say' \1 better written as $1 at -e line 1. bar foo
      We're told that backreference patterns can be used in the substitution by specifying \1, \2 (etc), but it doesn't seem to work.
      Who told you that?