Regular Expression

Hi,

In Perl What should be the regular expression for 1-23.
I tried with [1-9]|1[0-9]|2[0-3]. But it is not working.
I have a code snippet like below

$state = 0;
while( $state != 1 )
{
$hour=<STDIN>;
if ( $hour =~ /[1-9]|1[0-9]|2[0-3]/) {
                print "Integer within range.\n";
                $state = 1;
                }
                else
                {
                        print "out of range.\n";
                }
                }

But When I pass 0 it is saying "out of range" and when I pass 24 it is saying Integer with range.
I don't know what is the problem with it.
Can any one please take look and correct me?

Thanks,
Siba

do it simply, without regex.

$str="1-23"
@s = split /-/, $str;
if (  $var>=$s[0] && $var <=$s[1]){
  print "within range\n";
}

Sorry ghostdog74. I think I could not express my requirement propperly. Actually my requirement is The variable can contain at least 1 or at most 23. (Integer only.)

I need a regular expression which satisfy all the integer 1 though 23 including both 1 and 23.

If we try to assign any value less than 1 (Say 0 or -1 or .5) It should fail similarly if we try to assign any value greater than 23 (say 24 or 25 ...) it should fail. Similrly if we give any decimal value like 1.5 or 20.4 or 3.5 etc then also it should fail.

Thanks,
Siba

Your regex pattern: [1-9]|1[0-9]|2[0-3]

This results in a match (actually at least two matches) for "24", 2, and 4. You could try this:

^([1-9]|1[0-9]|2[0-3])$

With the start and end tags in there. Then "24" should not be matched.

This is a little easier to read and should not affect the speed of the parsing much:

^([1-9]|(1[0-9])|(2[0-3]))$