正则表达式中的专用IP地址标识符 [英] Private IP Address Identifier in Regular Expression

查看:86
本文介绍了正则表达式中的专用IP地址标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这是否是匹配以私有IP地址(Perl风格的正则表达式)开头的字符串的最佳方法:

I'm wondering if this is the best way to match a string that starts with a private IP address (Perl-style Regex):

(^127\.0\.0\.1)|(^192\.168)|(^10\.)|(^172\.1[6-9])|(^172\.2[0-9])|(^172\.3[0-1])

非常感谢!

推荐答案

我假设您要匹配以下范围:

I'm assuming you want to match these ranges:


127.  0.0.0 – 127.255.255.255     127.0.0.0 /8
 10.  0.0.0 –  10.255.255.255      10.0.0.0 /8
172. 16.0.0 – 172. 31.255.255    172.16.0.0 /12
192.168.0.0 – 192.168.255.255   192.168.0.0 /16

您遗漏了一些点,这些点会导致它接受例如172.169.0.0,即使它不应该被接受.我已经将其固定在下方.删除新行,仅出于可读性.

You are missing some dots that would cause it to accept for example 172.169.0.0 even though this should not be accepted. I've fixed it below. Remove the new lines, it's just for readability.

(^127\.)|
(^10\.)|
(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|
(^192\.168\.)

还请注意,这假设IP地址已经过验证-可以接受10.foobar之类的信息.

Also note that this assumes that the IP addresses have already been validated - it accepts things like 10.foobar.

这篇关于正则表达式中的专用IP地址标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆