Tried to search for an already answered question but ... couldn't find anything. I'm trying to make a regex that will match ipv4 or ipv4:port
$regex_ipv4 = '((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?![\\d])';
$regex_integer = '(\\d+)';
$x = "123.123.123.123";
$x2 = $x . ":12345";
preg_match("/^(?<ipv4>".$regex_ipv4.")(:)(?<port>".$regex_integer.")$/is", $x, $matches1);
preg_match("/^(?<ipv4>".$regex_ipv4.")(:)(?<port>".$regex_integer.")$/is", $x2, $matches2);
print_r($matches1);
print_r($matches2);
returns:
Array
(
)
Array
(
[0] => 123.123.123.123:12345
[ipv4] => 123.123.123.123
[1] => 123.123.123.123
[2] => 123.123.123.123
[3] => :
[port] => 12345
[4] => 12345
[5] => 12345
)
How can I make unconditional match of (:)(?<port>".$regex_integer.") ?
Thank you!
unconditional match of...?((:)(?<port>".$regex_integer."))??preg_match("/^(?<ipv4>".$regex_ipv4.")(?::(?<port>".$regex_integer."))?$/", $x, $matches);