i've been trying to write a concise regular expression for matching IP addresses. can someone please tell me why this works:
$okip = "25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]";
$estr = "($okip).($okip).($okip).($okip)$";
if (ereg($estr, $ip)) print "Valid IP";
/
output is as expected: 192.168.0.1 shows "Valid IP", while 192.168.0.256 does not
*/
but this code fails:
$okip = <same as above>
$estr = "($okip).($okip){3}$";
if (ereg($estr, $ip)) print "Valid IP";
This is the crazy part!: only ip addresses that look like 251.252.253.254 will pass the condiftional test. more specifically, only IP addresses where all segments match the first part of the "or" clause in the definition of $okip will work (250-255). this can be tested by rearranging the order of the parts in the "or" clause.
it looks like the rest of the "or" is not being evaluated.
am i doing something wrong? are my parentheses in the wrong places? is this the expected behavior of ereg?
i would appreciate any insight
thanks,
stephen