Forum Moderators: coopster

Message Too Old, No Replies

PHP Regular Expression To Match Numbers 1 to 553

those numbers are just an example...

         

rollinj

11:17 pm on Dec 16, 2009 (gmt 0)

10+ Year Member



Is it really as complicated as I think it is...?

I tried [1-553] but that only matched numbers 1 2 3 4 and 5. (As I specified numbers 1-5, and then respecified numbers 5 and 3...)

The only other method I can think of is incredibly time consuming... and goes something like:

if number is 1 digit, accept 1-9
if number is 2 digits, accept 1-9 for first digit, 0-9 for second digit
if number is 3 digits, accept 1-5 for first digit, 0-9 for the second and third digits, BUT if first number is 5, then second digit can only be a max of 5, and if that is true, then the last digit can only be a maximum of 3...

Has anybody come across a solution to this before? I'm not even sure of the search terms to help me find one...

TheMadScientist

12:31 am on Dec 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yeah, it's fairly complicated with just a regular expression, but depending on your specific situation you might be able to do something a bit different...

This might give you some ideas:


preg_match('#([0-9]{1,3})#',$string,$number);

if(isset($number[1]) && !empty($number[1]) {
$NumberToCheck=intval($number[1]);
if($NumberToCheck > 0 && $NumberToCheck <= 553) {
/* Do Stuff Here */
}
else {
echo 'Number Did Not Match';
}
}
else {
echo 'No Number Found';
}

rollinj

3:07 am on Dec 17, 2009 (gmt 0)

10+ Year Member



It does give me some ideas, thank you!

However, it almost entirely defeats the purpose of using a regular expression in the first place, does it not?

Is there a web scripting language that would accept the regex [1-553]? Or is there possibly (hopefully!) a more elegant way to do so in PHP?

I'm trying to match port:ip combinations, as quickly as possible. If that helps you at all.

The problem arises when I come across this combo for example:

123.123.123.123:99999

123.123.123.123 is a perfectly valid ip address, and the port is only 5 numbers long, which is normally fine, but a port can go no higher than 65535.

TheMadScientist

3:43 am on Dec 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm trying to match port:ip combinations, as quickly as possible. If that helps you at all.

Sure does: I wouldn't use a regular expression...
You can perform more specific checks than I am here but this is the base I would work from personally.


$IP = '123.123.123.123:99999';

$Check_IP_Port = explode(':',$IP);
$IP=explode('.',$Check_IP_Port[0]);

if( isset($Check_IP_Port) && !empty($Check_IP_Port[1])
&& intval($Check_IP_Port[1])>0 && intval($Check_IP_Port[1])<=65535
) {

echo 'Valid Port';
}
else {
echo 'Invalid Port';
}

if(is_array($IP) && count($IP) == 4
&& intval($IP[0]) > 0 && intval($IP[0]) <= 256
&& intval($IP[1]) > 0 && intval($IP[1]) <= 256
&& intval($IP[2]) > 0 && intval($IP[2]) <= 256
&& intval($IP[3]) > 0 && intval($IP[3]) <= 256
) {

echo 'Valid IP';
}

else {
echo 'Invalid IP';
}

EDITED: Variable Name & added an intval() I omitted from the first if & I really just like editing my posts... lol.

[1][edited by: TheMadScientist at 4:12 am (utc) on Dec. 17, 2009]

TheMadScientist

4:00 am on Dec 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you don't know you will always get a port, you could insert:


if(strpos($IP,':')!==FALSE) {
/* explode() on : here */
/* explode() on . here */
/* check [1] here */
}
else {
/* explode() on . here */
}

/* always check $IP here */

I'll let you figure out all the logic of the if / else in this post, but the preceding two posts are the fastest, simplest way I can think of to do what you are asking...

rollinj

7:33 am on Dec 17, 2009 (gmt 0)

10+ Year Member



I wouldn't use a regular expression...

I'm sorry, I wasn't entirely clear!

I'm extracting these ip:port combinations from a larger block of text, so I can't simply input the $IP because I don't know what it is yet.

I was using regular expressions find the ip:port combinations from the block of text using preg_match_all - I don't think there's any other way it can be done :S

Thank you oh so much for your help however, TheMadScientist! I will most likely end up using regular expressions to find them, and your php scripts to clean them up!

rollinj

8:42 am on Dec 17, 2009 (gmt 0)

10+ Year Member



I posted a related question here [webmasterworld.com], which deals with determining correct ip's via their hostname response...