for (my $candidate = 10234; $candidate <= 98765; ++$candidate) { # we want to jump eg from 10992 to 11000 # note not 'next': we've already incremented if the s/// succeeds redo if $candidate =~ s{ ^ (.*? (.) .*? \2) (.*) }{ ($1 + 1) . ("0" x length($3)) }ex; ... }