【代码保留】How to match the IP Address
今天看到微博上有人说有一种完美的IP判断方法,我就跑过去瞅了瞅,是PHP的,关键是用到了正则表达式。我本人对正则表达式可没好感了,因为它们的引擎需要解析那段规则(RegExp),然后把它们用通用的方式进行模式匹配,它的确给开发带来了便利,但对于判断一个字符串是否是IP地址的简单且通用的过程,我们有必要提供一种更定制化(量身定做)的方法用来做匹配。下面就是我用JavaScript写的一个DEMO,为了保证更好的移植性,代码以C风格书写,并考虑到一些C相关的特性(包括字符串char*无长度的概念,但也不可能直接用GCC来编译,毕竟是不同语言)
示例:(直接点击试用)
1 <script language="javascript" type="text/javascript">
2 function isIp( ipStr ) {
3 if(!ipStr) return false;
4
5 /*1.check length;*/
6 /*const*/ var minLengthLimit = 7; // 0.0.0.0
7 /*const*/ var maxLengthLimit = 15; // 255.255.255.255;
8 var length = stringGetLength(ipStr);
9 if(length < minLengthLimit || length > maxLengthLimit)
10 {
11 return false;
12 }
13
14 /*2.check base;*/
15 var lastIndex = length - 1;
16 var firstElement = ipStr.charAt(0);
17 var lastElement = ipStr.charAt(lastIndex);
18 if( firstElement == "." || firstElement < "0" || firstElement > "9"
19 || lastElement == "." || lastElement < "0" || lastElement > "9"){
20 return false;
21 }
22
23 var curNum = "";
24 var pointCounter = 0;
25 var numLength = 0; //for C language
26 for(var i = 0; i < length; ++i) {
27 var c = ipStr.charAt(i);
28 if( c >= "0" && c <= "9") {
29 curNum += c;
30 ++numLength;
31 if( numLength > 3) return false;
32 }
33 else if( c == ".") {
34 ++pointCounter;
35 if(pointCounter > 3)
36 return false;
37 if(numLength == 3) {
38 if(!checkLess255(curNum, 0)) return false;
39 }
40 if(pointCounter == 3) {
41 var lastNumLength = lastIndex - i;
42 if(lastNumLength > 3) return false;
43 var j = i + 1;
44 if(lastNumLength == 3)
45 {
46 if(!checkLess255(ipStr, j)) return false;
47 ++j;
48 }
49 for(; j < length; ++j) {
50 c = ipStr.charAt(j);
51 if( c < "0" || c > "9") return false;
52 }
53 return true;
54 }
55 curNum = "";
56 numLength = 0;
57 }
58 else return false;
59 }
60 }
61
62 function checkLess255( strNum, startIndex) {
63 var numLength = stringGetLength( strNum ) - startIndex;
64 if(numLength > 3) return false;
65 if(numLength == 3) {
66 var hundredPlace = strNum.charAt(startIndex);
67 if(hundredPlace > "2" /*|| hundredPlace < "0"*/) return false;
68 else if(hundredPlace == "2") {
69 var tenPlace = strNum.charAt(startIndex + 1);
70 if(tenPlace > "5") return false;
71 else if(tenPlace == "5") {
72 if(strNum.charAt(startIndex + 2) > "5") return false;
73 }
74 }
75 }
76 return true;
77 }
78
79 /*inline*/ function stringGetLength( str ) {
80 if(!str)
81 return 0;
82 else
83 {
84 try
85 {
86 return str.length;
87 }
88 catch(e)
89 {
90 return 0;
91 }
92 }
93 }
94
95 </script>
2 function isIp( ipStr ) {
3 if(!ipStr) return false;
4
5 /*1.check length;*/
6 /*const*/ var minLengthLimit = 7; // 0.0.0.0
7 /*const*/ var maxLengthLimit = 15; // 255.255.255.255;
8 var length = stringGetLength(ipStr);
9 if(length < minLengthLimit || length > maxLengthLimit)
10 {
11 return false;
12 }
13
14 /*2.check base;*/
15 var lastIndex = length - 1;
16 var firstElement = ipStr.charAt(0);
17 var lastElement = ipStr.charAt(lastIndex);
18 if( firstElement == "." || firstElement < "0" || firstElement > "9"
19 || lastElement == "." || lastElement < "0" || lastElement > "9"){
20 return false;
21 }
22
23 var curNum = "";
24 var pointCounter = 0;
25 var numLength = 0; //for C language
26 for(var i = 0; i < length; ++i) {
27 var c = ipStr.charAt(i);
28 if( c >= "0" && c <= "9") {
29 curNum += c;
30 ++numLength;
31 if( numLength > 3) return false;
32 }
33 else if( c == ".") {
34 ++pointCounter;
35 if(pointCounter > 3)
36 return false;
37 if(numLength == 3) {
38 if(!checkLess255(curNum, 0)) return false;
39 }
40 if(pointCounter == 3) {
41 var lastNumLength = lastIndex - i;
42 if(lastNumLength > 3) return false;
43 var j = i + 1;
44 if(lastNumLength == 3)
45 {
46 if(!checkLess255(ipStr, j)) return false;
47 ++j;
48 }
49 for(; j < length; ++j) {
50 c = ipStr.charAt(j);
51 if( c < "0" || c > "9") return false;
52 }
53 return true;
54 }
55 curNum = "";
56 numLength = 0;
57 }
58 else return false;
59 }
60 }
61
62 function checkLess255( strNum, startIndex) {
63 var numLength = stringGetLength( strNum ) - startIndex;
64 if(numLength > 3) return false;
65 if(numLength == 3) {
66 var hundredPlace = strNum.charAt(startIndex);
67 if(hundredPlace > "2" /*|| hundredPlace < "0"*/) return false;
68 else if(hundredPlace == "2") {
69 var tenPlace = strNum.charAt(startIndex + 1);
70 if(tenPlace > "5") return false;
71 else if(tenPlace == "5") {
72 if(strNum.charAt(startIndex + 2) > "5") return false;
73 }
74 }
75 }
76 return true;
77 }
78
79 /*inline*/ function stringGetLength( str ) {
80 if(!str)
81 return 0;
82 else
83 {
84 try
85 {
86 return str.length;
87 }
88 catch(e)
89 {
90 return 0;
91 }
92 }
93 }
94
95 </script>
PHP的那段代码我也复制过来了,正则嘛,总是特别短,精简,你一定也会很喜欢的,也许有的场合您更喜欢(同样也可以很容易转换成JavaScript):
1 function is_ip($str){
2 $ip = explode(”.”,$str);
3 for($i=0;$i<count($ip);$i++)
4 {
5 if($ip[$i]>255){
6 return (0);
7 }
8 }
9 return ereg(”^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$”,$str);
10 }
2 $ip = explode(”.”,$str);
3 for($i=0;$i<count($ip);$i++)
4 {
5 if($ip[$i]>255){
6 return (0);
7 }
8 }
9 return ereg(”^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$”,$str);
10 }
关键字:用C语言,判断字符串是不是IP
posted on 2009-11-12 00:42 volnet(可以叫我大V) 阅读(484) 评论(0) 编辑 收藏 举报