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

Thanks for that, it looked like complete gibberish 20 minutes ago but after some searching I have a handle on most of it now.

I've reduced it down to this:

exit 1 if $password =~ /(?x) ( \d ) (??{ ( $1 + 1 ) . ( $1 + 2 ) . ( $1 + 3 ) })/

Can this be easily adapted to allow an arbitrary number of consecutive digits? In my current code, the limit is configurable.

  • Comment on Re^2: Check a string for consecutive digits

Replies are listed 'Best First'.
Re^3: Check a string for consecutive digits
by johngg (Canon) on Nov 28, 2015 at 13:26 UTC

    Sorry for the slow reply, $work rather got in the way. Yes, it can be adapted quite easily:-

    use strict; use warnings; use 5.014; use re qw{ eval }; my $max = shift || 3; my $ascPatt = q{(?x) ( \d ) (??{ join q{}, map { $1 + $_ } 1 .. $max }) }; my $descPatt = q{(?x) ( \d ) (??{ join q{}, map { $1 - $_ } 1 .. $max }) }; my @passwords = qw{ 1234 1243 4321 298761 4562 4568 4578 123 12 1 01234 01243 04321 0298761 04562 04568 04578 0123 012 01 a1234 1a234 12a34 123a4 1234a a1b2c3 a12b34c56 a1b2c3d a12b34c56d a123b45c6 a12b345c6 a123b45c6d a12b345c6d 1a2 1ab2 12ab34 12abc34def 12abc34def567 abc ab12c 2345 234y 01234 2356 }; say qq{$_ - }, checkConsec( $_ ) ? q{too many consecutive digits} : q{pass} for @passwords; sub checkConsec { my $pw = shift; return 1 if $pw =~ m{$ascPatt}; return 1 if $pw =~ m{$descPatt}; return 0; }

    Three runs, the first using the default of no more than three consecutive digits, then four and two.

    I hope this is helpful.

    Cheers,

    JohnGG