q*s
3 楼
问题在于是用户输入的List,包括:
1. 单个的 IP 地址,比如, 192.168.3.230, 24.56. 88.99
2. 一个范围: 比如, 92.168.0.1-92.168.0.77
3. Subnet Mask: 192.168.10.0/24
有没有这样的算法,先把这些用户的输入(在文本文件中),读入,然后“整理”一下
,再用来检测?
多谢!
1. 单个的 IP 地址,比如, 192.168.3.230, 24.56. 88.99
2. 一个范围: 比如, 92.168.0.1-92.168.0.77
3. Subnet Mask: 192.168.10.0/24
有没有这样的算法,先把这些用户的输入(在文本文件中),读入,然后“整理”一下
,再用来检测?
多谢!
y*w
4 楼
还是像楼上说的,regex。
你允许的用户输入越复杂,你就需要越复杂的regular expression。搞明白你的规则先才是最重要的。正则表达式自己写不好的话,google一下,应该还是有不少的。
【在 q***s 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: 问题在于是用户输入的List,包括:
: 1. 单个的 IP 地址,比如, 192.168.3.230, 24.56. 88.99
: 2. 一个范围: 比如, 92.168.0.1-92.168.0.77
: 3. Subnet Mask: 192.168.10.0/24
: 有没有这样的算法,先把这些用户的输入(在文本文件中),读入,然后“整理”一下
: ,再用来检测?
: 多谢!
你允许的用户输入越复杂,你就需要越复杂的regular expression。搞明白你的规则先才是最重要的。正则表达式自己写不好的话,google一下,应该还是有不少的。
【在 q***s 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 问题在于是用户输入的List,包括:
: 1. 单个的 IP 地址,比如, 192.168.3.230, 24.56. 88.99
: 2. 一个范围: 比如, 92.168.0.1-92.168.0.77
: 3. Subnet Mask: 192.168.10.0/24
: 有没有这样的算法,先把这些用户的输入(在文本文件中),读入,然后“整理”一下
: ,再用来检测?
: 多谢!
q*s
5 楼
我的想法是对于那种 IP Range 的,是不是转换为整数来比较更快些?比如:
46.36.192.229 - 46.36.192.255,是否就可以转换为两个32位的整数,然后来比较其
他IP地址是否在这两个整数中间。(对ip的理解对否)
如果用regex来匹配,则上面的范围对应了4个cidr:
46.36.192.229/32
46.36.192.230/31
46.36.192.232/29
46.36.192.240/28
这样的话就有可能匹配4次。
再次感谢!
46.36.192.229 - 46.36.192.255,是否就可以转换为两个32位的整数,然后来比较其
他IP地址是否在这两个整数中间。(对ip的理解对否)
如果用regex来匹配,则上面的范围对应了4个cidr:
46.36.192.229/32
46.36.192.230/31
46.36.192.232/29
46.36.192.240/28
这样的话就有可能匹配4次。
再次感谢!
M*n
6 楼
Easy. Just convert an ip address into a number. Here is how:
43.36.192.229 ->
43*256*256*256 + 36*256*256 + 192*256 + 229.
To check if a given ip is within an ip range, you just need to figure out
starting, and ending ip address of the range. Then you convert them into
numbers. You know how to compare if a given number is within a number range.
right?
43.36.192.229 ->
43*256*256*256 + 36*256*256 + 192*256 + 229.
To check if a given ip is within an ip range, you just need to figure out
starting, and ending ip address of the range. Then you convert them into
numbers. You know how to compare if a given number is within a number range.
right?
q*s
7 楼
thanks a lot!
j*a
8 楼
给你1.1.1.500怎么办?
range.
【在 M*********n 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: Easy. Just convert an ip address into a number. Here is how:
: 43.36.192.229 ->
: 43*256*256*256 + 36*256*256 + 192*256 + 229.
: To check if a given ip is within an ip range, you just need to figure out
: starting, and ending ip address of the range. Then you convert them into
: numbers. You know how to compare if a given number is within a number range.
: right?
range.
【在 M*********n 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: Easy. Just convert an ip address into a number. Here is how:
: 43.36.192.229 ->
: 43*256*256*256 + 36*256*256 + 192*256 + 229.
: To check if a given ip is within an ip range, you just need to figure out
: starting, and ending ip address of the range. Then you convert them into
: numbers. You know how to compare if a given number is within a number range.
: right?
相关阅读
[合集] 帮我看看这个问题出在哪。overloading methodsHelp needed!JMX question问一下关于load image的servlethow to hide and show the cursor in JTextField?Classpath questions[合集] Is there any free java PDF viewer?xmlrpc apache java packagewhich IDE for GUI ?can someone recommend an html parser lib?请问关于用threadPoolExecutor实现threadpool的问题?Java Swing -- autohide panel widgets?爪哇的铜锈们新年好!做J2EE的大侠们,能推荐一下比较好用的referrence website吗?求助:什么是.do file呀JTabbedPane的一个问题[合集] Weiqi Gao: The Number One ill of JavaEclipse的CDT里如何生成tag? (转载)[合集] Swing问题:Auto hide task bar 如何实现?