use v5.10; my $input; local $" = ", "; my $parser = qr{ (?{ say "before:\n\@- = (@-)\t\t ".scalar(@-)." items\n\@+ = (@+)\t ".scalar(@+)." items\n"; }) ^ ((?&expr)) ((?&expr)) \z (?{ say "after:\n\@- = (@-)\t\t ".scalar(@-)." items\n\@+ = (@+)\t ".scalar(@+)." items\n"; }) (?(DEFINE) (? (.) (.) (?{ say "expr:\n\@- = (@-)\t ".scalar(@-)." items\n\@+ = (@+)\t ".scalar(@+)." items\n"; }) ) ) }x; $input = "abcd"; chomp($input); if ($input =~ $parser) { say "matches: ($&)"; say "At the very end:\n\@- = (@-)\t ".scalar(@-)." items\n\@+ = (@+)\t ".scalar(@+)." items\n"; } __END__ #### Compiling REx "%n (?{%n say %"before:\n\@- = (@-)\t\t %".scalar("... synthetic stclass "ANYOF[\0-\11\13-\377][{unicode_all}]". Final program: 1: EVAL (3) 3: BOL (4) 4: OPEN1 (6) 6: GOSUB3[+19] (9) 9: CLOSE1 (11) 11: OPEN2 (13) 13: GOSUB3[+12] (16) 16: CLOSE2 (18) 18: EOS (19) 19: EVAL (21) 21: DEFINEP (23) 23: IFTHEN (44) 25: OPEN3 'expr' (27) 27: OPEN4 (29) 29: REG_ANY (30) 30: CLOSE4 (32) 32: OPEN5 (34) 34: REG_ANY (35) 35: CLOSE5 (37) 37: EVAL (39) 39: CLOSE3 'expr' (44) 41: LONGJMP (43) 43: TAIL (44) 44: END (0) floating ""$ at 2..2147483647 (checking floating) stclass ANYOF[\0-\11\13-\377][{unicode_all}] minlen 2 with eval #### before: @- = (0) 1 items @+ = (0, , , , , ) 6 items expr: @- = (0, , , , 0, 1) 6 items @+ = (2, , , , 1, 2) 6 items expr: @- = (0, 0, , , 2, 3) 6 items @+ = (4, 2, , , 3, 4) 6 items after: @- = (0, 0, 2) 3 items @+ = (4, 2, 4, , , ) 6 items matches: (abcd) At the very end: @- = (0, 0, 2) 3 items @+ = (4, 2, 4, , , ) 6 items