如何获取IP:PORT与正则表达式? [英] How to grab IP:PORT with regex?
问题描述
好的,所以我在PHP中创建了一个小型IP:PORT scraper。问题是我对RegEx非常不熟悉。
Okay, so I'm creating a small IP:PORT scraper in PHP. Problem is that I'm pretty unfamiliar with RegEx.
所以我一直在拼凑我能做的事。
So I've been piecing together what I can.
这是我得到的:
/ \b(?:(?:25 [0-5] | 2 [0-4] [0-9] | [01 ?] [0-9] [0-9])\){3}(?: 25 [0-5] | 2 [0-4] [0-9] | [01] - [O- 9] [0-9]?):( [0-9] {1,5})\ b /
我知道这不是最好的。至少不是最终抓住端口,因为这意味着端口将能够像99999这样的东西。
I know this isn't the best. At least not the end to grab the port, because it means that ports will be able to be things like 99999.
此外,它似乎以这种方式返回两个匹配。 IP:PORT和PORT。我只需要它来获取完整的IP:PORT,而不是其中一个。
Also, it seems to return two matches this way. The IP:PORT and the PORT. I just need it to grab the full IP:PORT, not one or the other.
任何人都可以帮忙吗?
推荐答案
我已经在ip或ip和port匹配的下面发布了一个正则表达式。
I've posted a regular expression below what matches either ip or ip and port.
$ip = '111.222.333.444';
if ( preg_match('/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\:?([0-9]{1,5})?/', $ip, $match) ) {
echo 'ip: ' . $match['1'] . (isset($match['2']) ? ' port: ' . $match['2'] : '');
}
这篇关于如何获取IP:PORT与正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!