my $regex = qr# (\d) (??{ my $c //= $1 ; ++$c })+ #x; "2345654321" =~ $regex; print $&;