$_ = "04abcdefgh"; my $result; if (/ (\d\d) /gx) { $result .= $&; my $count = 0 + $1; if (/ \G .{$count} /x) { $result .= $&; } else { $result = undef } } dd $result; #-> "04abcd" #### "04abcdefgh" =~ / (\d\d) (??{ ". {".(0 + $^N)."}" }) /x; dd $&; #-> "04abcd" #### "04abcdefgh" =~ / (\d\d) (?> (?{ $^N }) # initialize $^R to $1 ( . (?(?{ --$^R }) (?-1)) # recurse if --$^R > 0 ) ) /x; dd $&; #-> "04abcd" #### "04abcdefgh" ~~ / (\d\d) . ** { $0 } /; #### "04abcdefgh" ~~ / (\d\d) . ** { $/[*-1] } /; #### "04abcdefgh" =~ / (\d\d) .{ (?{ $1 }) } /x; #### "04abcdefgh" =~ / (\d\d) .{ (?{ $^N }) } /x; #### ":aa2bb4cc6dd8" =~ / (:) (?: (\w\w) (\d) )* /x; dd $&; #-> ":aa2bb4cc6dd8" dd $1; #-> ":" dd $2; #-> "dd" dd $3; #-> 8 #### $_ = ":aa2bb4cc6dd8"; my @result; if (/ : /gx) { $result[0] .= $&; while (/ \G (\w\w) (\d) /gx) { $result[0] .= $&; push @{$result[1]}, $1; push @{$result[2]}, $2; } } dd $result[0]; #-> ":aa2bb4cc6dd8" dd $result[1]; #-> ["aa", "bb", "cc", "dd"] dd $result[2]; #-> [2, 4, 6, 8] #### ":aa2bb4cc6dd8" =~ / (:) (?{ [[], []] }) # initialize $^R (?: (\w\w) (\d) # add captures to $^R: (?{ [[@{$^R->[0]}, $2], [@{$^R->[1]}, $3]] }) )* /x; dd $&; #-> ":aa2bb4cc6dd8" dd $1; #-> ":" dd $^R->[0]; #-> ["aa", "bb", "cc", "dd"] dd $^R->[1]; #-> [2, 4, 6, 8] } #### ":aa2bb4cc6dd8" =~ / (:) (?{ [] }) # initialize $^R (?: (\w\w) (\d) # add captures to $^R: (?{ [@{$^R}, [$2, $3]] }) )* /x; dd $&; #-> ":aa2bb4cc6dd8" dd $1; #-> ":" dd $^R; #-> [["aa", 2], ["bb", 4], ["cc", 6], ["dd", 8]] #### ":aa2bb4cc6dd8" ~~ / (":") [ (\w\w) (\d) ]* /; dd $0.Str; #-> ":" dd $1».Str; #-> ("aa", "bb", "cc", "dd") dd $2».Int; #-> (2, 4, 6, 8) #### use re 'multi_captures'; ":aa2bb4cc6dd8" =~ / (:) (?: (\w\w) (\d) )* /x; dd $&; #-> ":aa2bb4cc6dd8" dd $1; #-> ":" dd $2; #-> ["aa", "bb", "cc", "dd"] dd $3; #-> [2, 4, 6, 8]