java IP地址面试题(正则解法)

题目描述–HW
请解析IP地址和对应的掩码,进行分类识别。要求按照A/B/C/D/E类地址归类,不合法的地址和掩码单独归类。

所有的IP地址划分为 A,B,C,D,E五类

A类地址1.0.0.0~126.255.255.255;

B类地址128.0.0.0~191.255.255.255;

C类地址192.0.0.0~223.255.255.255;

D类地址224.0.0.0~239.255.255.255;

E类地址240.0.0.0~255.255.255.255

私网IP范围是:

10.0.0.0~10.255.255.255

172.16.0.0~172.31.255.255

192.168.0.0~192.168.255.255

子网掩码为二进制下前面是连续的1,然后全是0。(例如:255.255.255.32就是一个非法的掩码)

输入描述:
多行字符串。每行一个IP地址和掩码,用~隔开。

输出描述:
统计A、B、C、D、E、错误IP地址或错误掩码、私有IP的个数,之间以空格隔开。

示例1
输入
10.70.44.68~255.254.255.0
1.0.0.1~255.0.0.0
192.168.0.2~255.255.255.0
19…0.~255.255.255.0
输出
1 0 1 0 0 2 1

这里我主要使用了正则判断,虽然简洁,但是正则需要非常仔细的写(比较花时间),需要仔细测试;
注意点:
1.正确的A类地址要满足:地址正确+掩码正确;
2.正确的私有IP地址要满足:地址正确+掩码正确
3.一个地址可以同时属于私有或者5类;
4.隐藏条件:0...以及127..*.*不属于任何类别。不算对,也不算错,需要过滤


import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Test;
public class IP地址面试题 {

	static int Aip=0;
	static int Bip=0;
	static int Cip=0;
	static int Dip=0;
	static int Eip=0;
	//错误IP地址或错误掩码
	static int errip=0;
	//私有IP的个数
	static int sip=0;
	
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		String input="";
		while(!(input=in.nextLine()).isEmpty()) {
				String s[]=input.split("~");
				get(s[0],s[1]);
		}
//		统计A、B、C、D、E、错误IP地址或错误掩码、私有IP的个数,之间以空格隔开。
		System.out.print(Aip+" "+Bip+" "+Cip+" "+Dip+" "+Eip+" "+errip+" "+sip);
	}

	//掩码
    private static boolean validMaskCode(String maskCode) {
    	String reg="(1+0+)";
        String code = Stream.of(maskCode.split("\\."))
              .map(Integer::valueOf)
              .map(s->{
            	 String bin= "00000000"+Integer.toBinaryString(s);
            	 return bin.substring(bin.length()-8);
              })
              .collect(Collectors.joining());
//        System.out.println(code);
        return code.matches(reg);
    }
     
	
	//IP
    static String r255_2 = "(\\.((\\d)|([1-9]\\d)|(([1]\\d{2})|(2(([0-4]\\d)|(5[0-5])))))){2}";
	static String r255_3 = "(\\.((\\d)|([1-9]\\d)|(([1]\\d{2})|(2(([0-4]\\d)|(5[0-5])))))){3}";
	 // 1-126
	static String ARegxp = "([1-9]|([1-9]\\d)|1(([0-1]\\d)|(2[0-6])))" + r255_3;
	 // 128-191
	static String BRegxp = "(1((2[8-9])|([3-8]\\d)|(9[0-1])))" + r255_3;
	 // 192-223
	static String CRegxp = "((19[2-9])|(2(([0-1]\\d)|(2[0-3]))))" + r255_3;
	 // 224-239
	static String DRegxp = "((22[4-9])|(23[0-9]))" + r255_3;
	 // 240-255
	static String ERegxp = "((24\\d)|(25[0-5]))" + r255_3;
//	私网IP范围是:
//	10.0.0.0~10.255.255.255
//	172.16.0.0~172.31.255.255
//	192.168.0.0~192.168.255.255
	static String pReg1 = "10" + r255_3;
	static String pReg2 = "(172\\.((1[6-9])|(2\\d)|(3[0-1])))" + r255_2;
	static String pReg3 = "(192\\.168)" + r255_2;
	//除外IP
	static String filterReg="(0|(127))"+r255_3;
	
	
	public static void get(String str, String yan) {
		//是否是正确掩码
		boolean flag = true;
		flag = validMaskCode(yan);
		//此题最坑处,为了和题目答案一致,
		//发现一个隐藏条件:0.*.*.*以及127.*.*.*不属于任何类别。不算对,也不算错
		if (flag) {
			if (str.matches(filterReg)) {
				return;
			}
		}
		
		if (str.matches(ARegxp) && flag) {
			Aip++;
		} else if (str.matches(BRegxp) && flag) {
			Bip++;
		} else if (str.matches(CRegxp) && flag) {
			Cip++;
		} else if (str.matches(DRegxp) && flag) {
			Dip++;
		} else if (str.matches(ERegxp) && flag) {
			Eip++;
		} else {
			errip++;
		}

		// 判断私网
		if ((str.matches(pReg1) || str.matches(pReg2) || str.matches(pReg3)) && flag) {
			sip++;
		}
	}
	
	
    //地址测试
	@Test
	public void testName() throws Exception {
		String s="(0|[127])";
		 //1-126
	    String ARegxp="([1-9]|([1-9]\\d)|1(([0-1]\\d)|(2[0-6])))";
	    //128-191
	    String BRegxp="(1((2[8-9])|([3-8]\\d)|(9[0-1])))";
	    //192-223
	    String CRegxp="((19[2-9])|(2(([0-1]\\d)|(2[0-3]))))";
	    //224-239
	    String DRegxp="((22[4-9])|(23[0-9]))";
	    //240-255
	    String ERegxp="((24\\d)|(25[0-5]))";
	    
	    for(int i=0;i<=127;i++) {
			  if ((i+"").matches(s)) {
				  System.out.println(i);
			  }
		  }
	    
	    
	    System.out.println("---------A------");
	  for(int i=1;i<=126;i++) {
		  if (!(i+"").matches(ARegxp)) {
			  System.out.println(i);
		  }
	  }
	  System.out.println("------B---------");
	  for(int i=128;i<=191;i++) {
		  if (!(i+"").matches(BRegxp)) {
			  System.out.println(i);
		  }
	  }
	  System.out.println("----------C-----");
	  for(int i=192;i<=223;i++) {
		  if (!(i+"").matches(CRegxp)) {
			  System.out.println(i);
		  }
	  }
	  System.out.println("--------D-------");
	  for(int i=224;i<=239;i++) {
		  if (!(i+"").matches(DRegxp)) {
			  System.out.println(i);
		  }
	  }
	  System.out.println("-------E--------");
	  for(int i=240;i<=255;i++) {
		  if (!(i+"").matches(ERegxp)) {
			  System.out.println(i);
		  }
	  }
	}
}


测试例子:

237.143.207.113~255.0.0.0
0.154.70.56~255.0.0.0
47.191.88.206~255.255.0.0
252.117.65.51~255.0.0.0
5.127.233.251~255.255.0.0
105.2.227.18~255.0.0.0
42.243.99.190~255.255.143.0
213.136.4.7~255.0.0.0
18.215.41.233~255.255.255.67
193.53.72.97~255.255.0.0
231.25.34.159~255.255.0.0
62.38.160.21~255.78.255.0
193.7.199.159~255.255.0.0
213.188.214.148~255.0.0.0
123.39.218.193~255.178.0.0
240.246.182.37~255.0.0.0
45.217.35.228~255.0.0.0
62.97.41.64~255.0.0.0
210.175.136.234~255.0.0.0
82.198.156.228~255.0.0.0
243.72.65.231~255.0.0.0
95.73.56.172~255.0.0.0
216.124.139.160~255.255.255.0
158.63.191.33~255.255.255.0
170.230.29.86~255.255.120.255
115.232.165.231~255.255.255.255
232.118.3.82~255.28.35.255
12.226.20.210~255.236.0.0
67.76.187.160~255.255.255.0
53.76.14.193~255.0.0.0
80.237.42.83~255.0.0.0
225.2.60.197~255.6.255.13
165.141.251.150~255.255.0.0
172.75.97.143~255.0.0.0
198.166.79.85~255.255.0.0
228.175.133.241~255.255.0.0
70.176.238.139~255.0.0.0
54.67.162.54~255.45.255.120
38.152.223.23~255.0.0.0
129.152.202.62~255.0.0.0
221.9.212.214~255.0.132.0
153.148.236.124~255.255.255.0
188.36.36.43~255.255.0.0
244.155.142.53~255.255.255.0
246.80.203.40~255.255.0.0
124.112.218.29~255.255.0.0
111.209.161.129~255.255.255.0
35.116.236.164~255.255.255.32
179.94.121.97~255.0.0.0
99.96.129.12~255.255.255.255
50.227.105.219~255.0.0.0
29.242.7.49~255.0.0.0
139.31.65.107~255.255.0.0
72.50.111.118~255.255.255.0
193.242.64.7~255.255.0.0
254.56.183.232~255.255.0.0
12.179.234.92~255.255.255.255
17.95.176.188~255.0.0.0
89.209.212.195~255.255.0.0
132.59.5.10~255.255.0.0
82.157.198.52~255.0.0.0
6.72.161.12~255.252.0.0
47.178.94.149~255.255.0.0
147.243.229.30~255.255.255.0
242.18.226.176~255.0.0.0
187.72.221.156~255.0.0.0
74.136.48.91~255.255.255.0
194.161.244.213~255.0.0.0
176.231.190.78~255.255.0.0
223.164.187.227~255.0.0.0
251.47.51.76~255.132.255.0
90.108.60.242~255.255.255.255
163.170.22.99~255.255.255.0
229.78.165.75~255.255.0.0
54.222.173.56~255.255.0.0
190.90.6.221~255.0.0.0
79.60.73.212~255.0.240.0
198.137.223.142~255.0.0.0
19.220.60.252~255.255.255.255
84.109.38.202~255.255.202.0
189.228.141.30~255.255.0.0
114.9.145.171~255.0.0.0
53.208.55.234~255.255.255.0
160.172.82.18~255.255.255.0
51.56.219.106~255.255.0.0
190.207.66.172~255.255.0.0
230.58.1.224~255.255.0.0
64.150.187.135~255.6.255.255
235.40.32.49~255.255.0.0
73.154.128.199~255.0.0.0
58.120.129.127~255.0.44.0
43.15.83.156~255.220.0.0
41.71.211.168~255.255.255.255
163.90.16.26~255.0.0.0
91.199.55.68~255.255.0.0
148.125.152.107~255.0.0.0
15.158.22.221~255.255.255.255
113.200.126.58~255.0.0.0
222.138.60.69~255.255.0.0
224.109.93.237~255.16.0.0
100.79.107.3~255.0.0.0
28.8.87.106~255.255.0.0
35.6.154.226~255.64.0.0
94.66.198.42~255.255.255.0
138.91.63.51~255.255.255.255
79.163.185.212~255.0.73.0
13.47.62.254~255.255.0.0
0.98.251.80~255.255.0.0
64.202.210.194~255.109.0.0
14.40.186.199~255.0.0.0
40.147.20.10~255.130.255.255
42.96.80.216~255.255.0.0
40.209.106.107~255.0.0.44
1.4.190.37~255.0.0.0
90.84.140.176~255.0.0.0
60.160.75.176~255.0.116.0
224.13.12.33~255.0.0.0
121.183.63.101~255.255.0.0
69.251.16.86~255.255.0.0
189.249.195.152~255.255.0.0
214.195.53.224~255.255.255.0
147.92.127.195~255.20.255.255
91.228.181.229~255.0.0.0
133.132.202.183~255.0.0.194
145.99.161.183~255.0.0.0
146.23.46.148~255.255.255.0
53.169.214.204~255.0.0.0
254.188.234.67~255.0.0.0
45.150.148.129~255.121.0.0
116.50.190.222~255.0.0.0
28.132.153.169~255.0.21.0
142.135.130.154~255.0.0.0
159.142.190.4~255.255.0.50
128.6.204.108~255.255.255.255
169.208.53.137~255.255.0.0
91.87.203.152~255.0.0.0
18.106.204.42~255.0.0.0
135.188.203.11~255.38.255.255
212.143.77.99~255.0.0.0
125.156.175.175~255.0.0.0
16.91.71.194~255.255.0.0
188.93.180.198~255.0.35.0
225.199.61.74~255.0.0.0
208.137.66.197~255.250.0.0
182.59.211.127~255.255.255.255
151.114.181.6~255.255.0.0
137.171.116.236~255.255.255.0
49.16.145.71~255.0.0.0
132.125.51.0~255.0.0.0
65.181.121.61~255.255.0.0
93.67.229.191~255.255.255.255
176.131.218.176~255.43.0.0
116.19.9.54~255.255.0.0
120.2.106.116~255.0.0.0
63.121.49.129~255.0.178.0
101.164.139.247~255.0.0.151
87.240.168.249~255.0.0.0
86.13.245.114~255.0.0.0
166.153.184.220~255.255.0.0
140.192.94.128~255.255.255.0
234.137.5.101~255.0.0.0
14.67.45.3~255.255.255.103
139.22.95.184~255.255.255.0
84.167.82.117~255.255.255.0
146.81.247.172~255.255.255.0
43.61.101.60~255.255.255.37
226.181.198.45~255.0.0.0
86.183.134.176~255.0.110.0
172.187.201.37~255.255.255.0
78.92.195.189~255.255.255.58
49.166.53.50~255.255.255.255
217.49.211.121~255.0.0.0
79.247.26.197~255.0.0.0
41.107.42.174~255.0.0.30
245.215.75.184~255.255.0.0
207.2.79.93~255.0.0.0
137.180.128.248~255.255.0.0
218.131.220.8~255.0.0.0
63.81.15.106~255.255.255.0
117.199.49.82~255.0.0.0
72.32.205.233~255.0.0.0
60.102.39.147~255.229.255.0
141.13.125.188~255.0.205.0
192.70.108.29~255.0.0.0
50.110.229.178~255.255.0.63
168.184.56.244~255.167.255.0
183.229.153.179~255.255.0.0
142.11.227.97~255.255.255.255
128.230.188.51~255.0.0.0
115.115.173.90~255.255.255.0
0.225.159.70~255.255.0.0
148.224.29.42~255.255.0.0
253.60.17.23~255.255.255.0
97.129.31.87~255.0.0.0
145.206.229.167~255.0.0.0
179.178.201.165~255.0.0.0
60.6.47.94~255.255.0.0
97.42.90.152~255.158.255.255
219.59.196.192~255.255.0.0
228.16.173.236~255.255.0.232
238.124.61.67~255.0.0.0
148.223.212.228~255.0.193.0
198.177.212.136~255.255.119.255
67.172.177.72~255.0.0.0
82.54.109.193~255.255.0.0
170.75.209.121~255.0.0.0
241.102.6.161~255.255.255.255
107.164.39.154~255.255.255.255
10.208.59.135~255.255.0.0
160.90.191.172~255.255.0.0
218.92.24.2~255.0.0.0
22.221.180.118~255.0.0.0
136.191.32.169~255.0.203.0
89.29.188.169~255.0.0.0
119.179.89.216~255.255.255.255
117.14.180.194~255.0.0.0
12.152.112.19~255.131.255.0
113.12.58.159~255.255.0.0
216.28.65.9~255.255.0.0
155.57.46.56~255.74.255.0
116.88.236.253~255.0.0.0
62.171.35.217~255.0.0.0
19.13.187.141~255.0.0.0
144.166.52.157~255.5.255.255
234.193.80.85~255.255.0.0
179.181.205.29~255.0.0.0
回车
71 41 21 12 10 68 1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值