http://qs1969.pair.com?node_id=11140601


in reply to Refer by relative way to regex group (eg. the last one)

You may wanna try the two possibilities listed in perlretut and perlvar

You got those links already last time you asked.

Additionally has perlre even a demo similar to your code.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

  • Comment on Re: Refer by relative way to regex group (eg. the last one)

Replies are listed 'Best First'.
Re^2: Refer by relative way to regex group (eg. the last one)
by RMGir (Prior) on Jan 19, 2022 at 14:28 UTC
    The \g{-1} syntax is interesting, I don't recall that one. When did it come about? (Of course, it's possible it's been around since Perl 4 and I just missed it :) )

    Mike

      Looks like 5.10 according to perl5100delta:

      Relative backreferences A new syntax "\g{N}" or "\gN" where "N" is a decimal integer a +llows a safer form of back-reference notation as well as allowing re +lative backreferences. This should make it easier to generate and emb +ed patterns that contain backreferences. See "Capture buffers" in perlre. (Yves Orton)

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

      Grepping the perldelta* files for \\g lists perl5100delta, so likely the feature was introduced with Perl 5.10.0.

      Syntax::Construct to the rescue :-)

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      Perl 5.9.5 if you want to be picky.