Environment: Perl 5.26.2 x64 on Cygwin x64.
Question: after my $re = qr/...(capturing group).../, is there any way to use $re without capturing into its capturing groups?
X: I am matching lines that could either be:
#define FOO(X,Y) SomeComplicatedStuff
or
#define FOO(X,Y) BAR(X,Y)
I have a compiled regex $re that matches FOO(X,Y) and includes numbered capturing groups to split the match into FOO and X,Y. I would like to match lines of the second form without having to define a separate regex, e.g., using m/$re.+$re/. This works fine, but I get all the capturing groups of FOO when I all I really want are the groups of BAR.
Y: I thought I could do this in 5.22+ with the /n modifier, but I can't get it to work. MCVE:
$ perl -E 'my $re=qr/(foo|bar)/; "foobar" =~ m/$re$re/; say $1, " ", $2;'
foo bar # as expected
$ perl -E 'my $re=qr/(foo|bar)/; "foobar" =~ m/(?n:$re)$re/; say $1, " ", $2;'
# I think this should turn off ^^^ capturing of `foo`
foo bar # oops - I was hoping for `bar`
$ perl -E 'my $re=qr/(foo|bar)/; "foobar" =~ m/(?n:(foo|bar))$re/; say $1, " ", $2;'
bar # This works, but I had to inline $re within (?n:...).
Note: I also tried \K:
$ perl -E 'my $re=qr/(foo|bar)/; "foobar" =~ m/$re\K$re/; say $1, " ", $2, " ", $&;'
foo bar bar # was hoping for `bar bar`
Edit Forgot to mention — I did look at this related question, but it's not the same problem statement.