2
试图搜索已经回答的问题,但...找不到任何东西。 我试图做一个正则表达式将匹配IPv4或IPv4的:端口REGEX:无条件匹配
$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);
回报:
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
)
我怎样才能使(:)(?<port>".$regex_integer.")
无条件的比赛?
谢谢!
什么你的意思是“无条件地匹配......”吗? –
'((:)(?“。$ regex_integer。”))?'? –
看起来代码正在工作,命名组捕获你所需要的。 –