As far as I can tell, when using a purely regex solution, you pretty much have to keep the scanning for ascending and descending sequences separated. Otherwise overlapping sequences will be missed:
123xxxx56543
Capturing in one go will return qw(123 56 543) instead of qw(123 56 6543);
my $asc = join '|', map {"$_(?=".(($_+1)%10).")"} (0..9);
my $dsc = join '|', map {"$_(?=".(($_+9)%10).")"} (0..9);
while (<DATA>) {
my $longest = max map length, /((?:$asc)+)/g, /((?:$dsc)+)/g;
print "$longest\n";
}
Update: This was in reference to your last solution, as previous ones did take this into account.