RegEx Validation for a 01<x<32

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
bitz21
Forum Newbie
Posts: 3
Joined: Thu Aug 17, 2006 2:18 am

RegEx Validation for a 01<x<32

Post 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
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post 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";
bitz21
Forum Newbie
Posts: 3
Joined: Thu Aug 17, 2006 2:18 am

RegEx Validation for a 01<x<32

Post 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
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

untested:

Code: Select all

preg_match('/^(?[012]\d|3[012])$/', $x)
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post 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  */

}
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

@blackbeard,

What about '29'? It is a two digit number between 01 and 32 which would fail under your regex.
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post 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.
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post 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'));
}
bitz21
Forum Newbie
Posts: 3
Joined: Thu Aug 17, 2006 2:18 am

RegEx Validation for a 01<x<32

Post 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
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post 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
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Re: RegEx Validation for a 01<x<32

Post 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.
mator
Forum Newbie
Posts: 11
Joined: Sat Aug 19, 2006 1:23 am

Post 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
Post Reply