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:  <%-{-{-{-<


In reply to Re: Check a string for consecutive digits by AnomalousMonk
in thread Check a string for consecutive digits by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.