in reply to Check a string for consecutive digits

Another way, and configurable, but uses Perl 5.10 regex extensions:

c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; my $too_many_consec = qr{ (?(?{ index('0123456789', $^N) < 0 && index('9876543210', $^N) < 0 +}) (*FAIL)) }xms; ;; my $min = 4; for my $s (qw( 123 321 2123 2321 1232 3212 21232 23212 1234 4321 21234 34321 12343 43212 212343 343212 12345 54321 212345 454321 123454 543212 2123454 4543212 )) { printf qq{'$s' -> }; print $s =~ m{ (\d{$min,10}) $too_many_consec }xms ? qq{too many consec: '$1'} : 'consec free' ; } " '123' -> consec free '321' -> consec free '2123' -> consec free '2321' -> consec free '1232' -> consec free '3212' -> consec free '21232' -> consec free '23212' -> consec free '1234' -> too many consec: '1234' '4321' -> too many consec: '4321' '21234' -> too many consec: '1234' '34321' -> too many consec: '4321' '12343' -> too many consec: '1234' '43212' -> too many consec: '4321' '212343' -> too many consec: '1234' '343212' -> too many consec: '4321' '12345' -> too many consec: '12345' '54321' -> too many consec: '54321' '212345' -> too many consec: '12345' '454321' -> too many consec: '54321' '123454' -> too many consec: '12345' '543212' -> too many consec: '54321' '2123454' -> too many consec: '12345' '4543212' -> too many consec: '54321'
Extended Patterns used are  (?(condition)yes-pattern|no-pattern) and  (*FAIL) (see Special Backtracking Control Verbs).

Updates:

  1. Actually, the upper limit of 10 in
        m{ (\d{$min,10}) $too_many_consec }xms
    is not needed, and this works just as well written as
        m{ (\d{$min,}) $too_many_consec }xms
    instead. The unneeded upper limit might even be regarded as a potential future pitfall: what if you extend this approach to alphabetic consecutive runs and forget to change the limit?
  2. Another afterthought: The regex object  $too_many_consec is misnamed. The "too many" function is fulfilled by the lower limit  $min of the counted quantifier; the regex following the capture group only tests for (and fails in the absence of) consecutivity in the capture, and so should be named something like  $consecutive instead.


Give a man a fish:  <%-{-{-{-<