Forum Moderators: coopster

Message Too Old, No Replies

Regex newbie question

it's should be a simple one

         

le_gber

10:32 pm on Nov 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just a quick one,

I am using a regex to check that a number is between 0 and 20

So I use '/^[0-9]{0,2}$/i' as the pattern.

I thought I could use '/^[0-9]{1,2}$/i' as the number will be at have 1 occurence of the [0-9] but it throws some error.

anyone cares to explain why?

cheers

eelixduppy

11:16 pm on Nov 6, 2006 (gmt 0)



Your logic isn't going to work. It will match anything that is between 0 and 99, not 0 and 20. Why aren't you just using a simple comparison?:

if($num >= 0 && $num <= 20)...

ramoneguru

6:54 am on Nov 7, 2006 (gmt 0)

10+ Year Member



Well, if you have to use a regex:

<?php
$pattern = "/^([0-9]¦1[0-9]¦20)$/";
$num = 0;
while ($num < 100) {

if (preg_match($pattern, $num) ) {
echo "$num was a value 0-20<br/>\n";
} else {
echo "$num was not a value 0-20<br/>\n";
}

$num++;
}
?>
--Nick

if you can allow things like '00' or '09' then this one will work as well:
$pattern = "/^([01]?[0-9]¦20)$/";

le_gber

9:01 am on Nov 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



thanks guys.

it doesn't really matter if the number is outside 20, I just don't understand why it throws an error if I use {1,2} instead of {0,2} for the length?

I should say that it's to check the value of the numerical captcha I've built (same type of what's on Matt Cutts page). I just want to make sure it's a number of no more that 2 digits.

coopster

2:33 pm on Nov 7, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The pattern works fine for me, no errors, with either {0,2} or {1,2}. What error are you receiving? Which version of PHP?

le_gber

9:04 am on Nov 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



coopster,

It's not an error per se (i.e not a php error message), but the 'captcha' is supposed to be validated and block form submission until both numbers are the same. When I use the {1,2} and leave the captcha form field empty, the form goes through. When I use the {0,2} it behaves as it should.

ramoneguru

9:31 am on Nov 8, 2006 (gmt 0)

10+ Year Member



Hmmmm, not sure about your error but that regular expression says:

'/^[0-9]{0,2}$/i'

Go to the beginning of the string, allow a digit 0-9 of at least 0 length and at most a length of 2 (so that means NULL - 99 will work). Then go to the end of the string........ then use case-insensitive for whatever reason. Why is that in there? Can you do a case-insensitive search with numbers? That's not sarcasm, I'm genuinely interested.
--Nick

le_gber

9:38 am on Nov 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ramoneguru, I guess I don't know regex as well as you :) the i at the end (which I assume is what you are talking about) came from another expression that checks text. I guess I didn't know what it was and forgot to remove it. Thanks for pointing it out.

le_gber