in reply to Re: Check a string for consecutive digits
in thread Check a string for consecutive digits

This another variant that uses qr// to check for the 'or' / '|' of all three digit sequences. I just boil it down to one regex here.

use strict; use warnings; my $dig3_regex_str = join '|', map { ($_, scalar reverse $_) } # 012 and 210 map { join '', $_ .. $_+ 2 } 0..7; # 012, 123, ... my $dig3_regex = qr/$dig3_regex_str/; # Test samples taken from other monks postings ... my @strings = ( '10203040', '1234', '298761', '4562', '856423', 'a12b543c'); for my $string (@strings) { print "Bad string => $string\n" if $string =~ $dig3_regex; }
Ron

Replies are listed 'Best First'.
Re^3: Check a string for consecutive digits
by AnomalousMonk (Archbishop) on Nov 26, 2015 at 18:48 UTC

    Your more neat (IMHO) variant is easily made configurable and, like Re: Check a string for consecutive digits, uses no 5.10 regex extensions:

    use constant MIN => 4; my $delta = MIN-1; ;; my ($too_many_consec) = map qr{ $_ }xms, join ' | ', map { $_, scalar reverse $_ } map { join '', $_ .. $_+$delta } 0 .. (9-$delta) ; ...


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