'The inserted conditional code regex for Raku

How to do the inserted conditional code regex in Raku regex
As analogue to its Perl regex

 my $F = 1;
 'foobarbar' =~ / (?(?{  $F  }) foo |  bar ) bar /x  ;

Please help out after tried so hard a day to no avail, thanks.



Solution 1:[1]

This will work:

my $F=1
'foobar' ~~ / ^^ "{ $F ?? "foo" !! "bar" }" bar /; # ?foobar?
$F=0
'foobar' ~~ / ^^ "{ $F ?? "foo" !! "bar" }" bar /; # Nil

Code blocks in regexes will be run, but unless you convert them explicitly to strings (via quotes) they will be discarded.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jjmerelo