如何将大字符串中的子字符串与正则表达式匹配?

时间:2014-06-02 18:29:15

标签: php regex

我的端口也是[2401:2401:2401:2401:2401:2401:2401:2401]:1234的IPv6地址。我想只在PHP中获取IPv6地址。我想做的是:

$str = "[2401:2401:2401:2401:2401:2401:2401:2401]:1234";
preg_match("[]", $str, $matches);
echo $matches[1];

但这并没有让我得到正确的结果。 我希望matches[1]仅包含"2401:2401:2401:2401:2401:2401:2401:2401"

1 个答案:

答案 0 :(得分:1)

您使用的preg_match无法正常工作。你需要这样的东西:

preg_match("/\[([0-9a-fA-F:]+)\]/", $str, $matches);