Jump to content


Photo

Regex and consecutive numbers


  • Please log in to reply
4 replies to this topic

#1 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 06 July 2006 - 07:37 PM

The aim of the exercise is to ban users from selecting weak PINs. Each PIN starts with 1 uppercase letter and ends with 4 numbers. eg A2587. However, a PIN such as A1234 could be easily guessed. Therefore, my questions are:

1. How do i go about creating an expression that matches the uppercase letter and 4 consecutive numbers? eg A2345, B2345, C2345, etc

2. How do i make the expression sensitive to ascending or descending order?
eg A2345, A5432 or B2345, B5432.


Thanks in advance


#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 06 July 2006 - 08:12 PM

<?php

		$tests = array(
			### Should Fail.
			'A2345',
			'B2345',
			'C2345',
			'A5432',
			'B4321',
			'C0123',
			### Should Pass.
			'A1235',
			'B2367',
			'C5431',
			'A1385',
			'B9870',
			'C1111',
		);
	
		foreach ($tests as $test) {
			echo "<b>Testing '$test':</b>";
			### Match the digits on the ends (the first and fourth).
			preg_match('/(\d).+(\d)/', $test, $digits);
			### Get rid of the full pattern match.
			array_shift($digits);
			### There is no consecutive pattern if they are equal.
			if ($digits[0] == $digits[1]) {
				echo 'Not Consecutive';
				continue;
			}
			### Find the high and low; assign them to variables.
			if ($digits[0] < $digits[1]) {
				list($low, $high) = $digits;
			}
			else {
				list($high, $low) = $digits;
			}
			### Create a range.
			$range = range($low, $high);
			### There are only 4 numbers, so if they are consecutive our
			### range should be 4.
			echo count($range) == 4 ? 'Consecutive' : 'Not Consecutive' ;
			echo '<br />';
		}
?>

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 Wildbug

Wildbug
  • Members
  • PipPipPip
  • Advanced Member
  • 1,149 posts

Posted 06 July 2006 - 08:18 PM

Aren't there only twelve possiblities?

1234, 2345, 3456, 4567, 5678, 6789
9876, 8765, 7654, 6543, 5432, 4321

preg_match('/1234|2345|3456|4567|5678|6789|9876|8765|7654|6543|5432|4321/',$pin)

It's not elegant, but it's easier than trying to make an abstract search pattern, which I don't think can be done in one step with regex.
Twice a day my clock works PERFECTLY!  I can't figure out what's wrong with it.

#4 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 06 July 2006 - 08:52 PM

Wildbug your a genius. Its much better to just eliminate the 12 possible combinations from consideration that leaves roughly 114 remaining combinations.

Thanks


#5 Wildbug

Wildbug
  • Members
  • PipPipPip
  • Advanced Member
  • 1,149 posts

Posted 07 July 2006 - 03:53 PM

I don't know if you're already checking for it, but one simple regex you can use to check for repetition is /(\d)\1\1\1/ -- this will find pins with all the same numbers ('1111', '2222', etc).
Twice a day my clock works PERFECTLY!  I can't figure out what's wrong with it.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users