描述
IP地址的长度为32位,分为4段,每段8位,用十进制数字表示,每段数字范围为0~255,段与段之间用英文句点 "." 隔开。
分析IP地址的组成特点:250-255、200-249、0-199。 分开考虑:
- 250-255:特点:三位数,百位是2,十位是5,个位是0~5,用正则表达式可以写成:25[0-5]
- 200-249:特点:三位数,百位是2,十位是04,个位是09,用正则表达式可以写成:2[0-4]
- 0-199:这个可以继续分拆,这样写起来更加简单明了.
- 0-9: 特点:一位数,个位是0~9,用正则表达式可以写成:
- 10-99: 特点:二位数,十位是19,个位是09,用正则表达式可以写成:[1-9]
- 100-199:特点:三位数,百位是1,十位是09,个位是09,用正则表达式可以写成:1
于是正则为 (([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#!/bin/bash #----------------------------------------------------------- # Usage: 正则匹配ip # $Id: ip-reg.sh i@annhe.net 2015-08-22 03:17:35 $ #----------------------------------------------------------- reg=( "^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$" #"^(([1]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}([1]?\d{1,2}|2[0-4]\d|25[0-5])$" "^(([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])$" ) function match_ip() { ip=$1 for id in ${reg[*]};do echo $ip | grep --color -P "$id" &>/dev/null && echo "ok $ip" || echo "failed $ip" done echo "" } function gen_test_data() { sum=$1 arr=() for ((i=0;i<$sum;i++));do let arr[0]=$RANDOM%300 let arr[1]=$RANDOM%200 let arr[2]=$RANDOM%100 let arr[3]=$RANDOM%10 let arr[4]=$RANDOM%1 let arr[5]=$RANDOM%5 let arr[6]=$RANDOM%500 let arr[7]=$RANDOM%350 let arr[8]=$RANDOM%150 let arr[9]=$RANDOM%1000 ip=() for ((j=0;j<4;j++));do let index=$RANDOM%9 ip[$j]=${arr[$index]} done ip="${ip[0]}.${ip[1]}.${ip[2]}.${ip[3]}" match_ip $ip done } gen_test_data 20 list=( 0.0.0.0 255.255.255.255 001.02.5.3 124.0.0.0 299.9.1.2 ) for id in ${list[@]};do match_ip $id done |
测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
[root@HADOOP-215 meituan]# ./ip-reg-meituan.sh ok 41.174.174.8 ok 41.174.174.8 ok 38.19.19.0 ok 38.19.19.0 ok 88.116.0.0 ok 88.116.0.0 failed 0.426.192.76 failed 0.426.192.76 ok 242.0.251.251 ok 242.0.251.251 failed 9.86.9.321 failed 9.86.9.321 failed 487.194.3.0 failed 487.194.3.0 failed 66.3.3.482 failed 66.3.3.482 ok 6.110.110.108 ok 6.110.110.108 ok 1.143.35.1 ok 1.143.35.1 ok 98.211.43.0 ok 98.211.43.0 ok 1.230.9.131 ok 1.230.9.131 ok 16.120.2.0 ok 16.120.2.0 failed 0.329.88.85 failed 0.329.88.85 ok 243.123.2.31 ok 243.123.2.31 ok 198.2.28.17 ok 198.2.28.17 ok 126.238.189.238 ok 126.238.189.238 ok 7.34.213.7 ok 7.34.213.7 failed 362.0.321.33 failed 362.0.321.33 ok 31.7.0.60 ok 31.7.0.60 ok 0.0.0.0 ok 0.0.0.0 ok 255.255.255.255 ok 255.255.255.255 ok 001.02.5.3 failed 001.02.5.3 ok 124.0.0.0 ok 124.0.0.0 failed 299.9.1.2 failed 299.9.1.2 |
参考资料
1 2 |
[1]. IP地址的正则表达式. http://www.cnblogs.com/txw1958/archive/2011/10/13/ip_address_regular_expression.html [2]. 正则表达式30分钟入门教程. http://deerchao.net/tutorials/regex/regex.htm |
发表回复