Using your code, the compiler generates the following warning:
Regex object coerced to string (please use .gist or .perl to do that)
This tells us something wrong: the regular expression should not be treated as strings. There are two correct ways to place regular expressions. First, you can include sub-submissions in statements ( <>
):
my $str = 'nn12abc34efg';
my Regex $atom = / \d ** 2 /;
my Regex $rgx = / (<$atom>) \w+ (<$atom>) /;
$str ~~ $rgx;
Please note that I do not match / $rgx /
. This puts one regular expression inside another. Just match $rgx
.
. atom
, , $<atom>[0]
$<atom>[1]
:
my regex atom { \d ** 2 };
my $rgx = / <atom> \w+ <atom> /;
$str ~~ $rgx;