How can I use capturing groups inside lookbehind assertions?
I tried to use the same formula as in this answer. But that does not seem to work with lookbehinds.
Conceptually, this is what I was trying to do.
say "133" ~~ m/ <?after $0+> (\d) $ /
I know this can be easily achieved without lookbehinds, but ignore that just for now :)
For this I tried with these options:
Use :var
syntax:
say "133" ~~ m/ <?after $look-behind+> (\d):my $look-behind; $ /;
# Variable '$look-behind' is not declared
Use code block
syntax defining the variable outside:
my $look-behind;
say "133" ~~ m/ <?after $look-behind+> (\d) {$look-behind=$0} $ /;
# False
It seems that the problem is that the lookbehind
is executed before the "code block/:my $var", and thus the variable is empty for the lookbehind tree.
Is there a way to use capturing groups inside lookbehinds?
say .<after> given foo ~~ m/ $ <after bar> /
will work for any string infoo
that ends with the patternbar
, which is fair enough... and displays a capture that's the string infoo
in its entirety (not justbar
) and entirely flipped (backwards)! 🤪 – Heinrick/ (a) :my $lookahead; <?before b {$lookahead = $/}> /
), a simple translation of that to a lookbehind version solving this Q would be/ (a) :my $lookbehind; { $lookbehind = $/ } <?after $lookbehind ** 2> $ /;
. (Which is more or less the same as .@WiktorStribiżew's answer.) – Heinrick