Page 1 of 1

RegEx Validation for a 01<x<32

Posted: Thu Aug 17, 2006 2:23 am
by bitz21
Hi I'm trying to validate a number that comes within this range

01 < x < 32

x should always be a 2 digit number.

Can anyone help me with the regEx expression that will help me validate X

Thanks,
Bitz

Posted: Thu Aug 17, 2006 6:26 am
by blackbeard
If I understand you right, you need to make sure that string $x fits a particular format (two numbers), and then you need to make sure that it's value falls between 1 and 32. If so, you can try this:

Code: Select all

if (preg_match("[0-9]{2}", $x))  // test $x to make sure it's two digits
  if (intval($x) >= 1  && intval($x) <=32)  //  it's between 1 and 32
    echo $x."It's a match";
  else
    echo "Not a match";

RegEx Validation for a 01<x<32

Posted: Thu Aug 17, 2006 6:38 am
by bitz21
Hi BlackBeard,
Thanks for your response.

Actually what i was looking for is that the RegExpression determines whether X a 2 digit number or not and also if it falls within the specified range.

Is there a way to do it ?


Thanks
Bitz

Posted: Thu Aug 17, 2006 7:02 am
by sweatje
untested:

Code: Select all

preg_match('/^(?[012]\d|3[012])$/', $x)

Posted: Thu Aug 17, 2006 7:03 am
by blackbeard
Maybe this:

Code: Select all

if (preg_match("^[0-3][0-2]", $x))  { // test $x to make sure the first digit is 0 to 3, the second is 0 to 2

/*  do whatever  */

}

Posted: Thu Aug 17, 2006 7:10 am
by sweatje
@blackbeard,

What about '29'? It is a two digit number between 01 and 32 which would fail under your regex.

Posted: Thu Aug 17, 2006 7:15 am
by blackbeard
Yep, you're right. I think that's why I went with my first choice, to narrow it down to at least two digits, and then using the second if to make sure it fell in the right value range.

Posted: Thu Aug 17, 2006 7:17 am
by sweatje
Ok, tested now and expressed as a SimpleTest test method:

Code: Select all

function testPregToMatchTwoDigitNumberLessThan32() {
	$regex = '/^(?:0[1-9]|[12]\d|3[012])$/';
	foreach(range(1,32) as $i) {
		$this->assertTrue(preg_match($regex,sprintf('%02d',$i)));
	}
	$this->assertFalse(preg_match($regex,'001'));
	$this->assertFalse(preg_match($regex,'00'));
	$this->assertFalse(preg_match($regex,'33'));
	$this->assertFalse(preg_match($regex,32.1));
	$this->assertFalse(preg_match($regex,' 08'));
	$this->assertFalse(preg_match($regex,'18 '));
	$this->assertFalse(preg_match($regex,'foo'));
}

RegEx Validation for a 01<x<32

Posted: Fri Aug 18, 2006 12:07 am
by bitz21
Hi,
Sweatje,
Thanks for giving me the solution. This is exactly what i was looking for.
Could you please take some more time out and tell me what is happening here? Is it one of those lookbehind and lookahead stuff ?

Thanks
Bitz

Posted: Fri Aug 18, 2006 7:08 am
by sweatje
/^(?:0[1-9]|[12]\d|3[012])$/

/ delimiter for the regex
^ start of the string
(?: start a non-matching group
0[1-9] 0 followed by 1 through 9
| or
[12]\d 1 or 2 followed by any digit
| or
3[012] 3 followed by 0, 1 or 2
) end of the group
$ end of the string
/ end of the regex

Re: RegEx Validation for a 01<x<32

Posted: Fri Aug 18, 2006 10:34 am
by bokehman
bitz21 wrote:Hi I'm trying to validate a number that comes within this range

01 < x < 32

x should always be a 2 digit number.

Can anyone help me with the regEx expression that will help me validate X

Thanks,
Bitz
Integers dont have zero padding so you must be looking for a string. Is that correct? Are "01" and "32" fixed values.

Posted: Sat Aug 19, 2006 2:41 am
by mator
If 01 and 32 are included:

Code: Select all

0[1-9]|[12]\d|3[0-2]
If not:

Code: Select all

0[2-9]|[12]\d|3[01]
This will only match said values and is also as efficient as it gets