Overview

We use IP addresses in various situations. It is necessary to check the IP address that users enter incorrectly and prevent errors. The following is an example of checking whether IPv4 and IPv6 is a normal format, and in the case of IPv6, if it is long format, it is converted to a universal short format.

IPv6 can be abbreviated for 0, so the following addresses are the same.

1:ab8:18a3:0000:0000:8a2e:0120:3
1:0ab8:18a3:0000:0000:8a2e:120:3
1:ab8:18a3:0000:0000:8a2e:120:3
1:ab8:18a3::8a2e:120:3
1:0ab8:18a3::8a2e:0120:3
1:ab8:18a3::8a2e:0120:3
1:0ab8:18a3::8a2e:120:3
1:ab8:18a3::8a2e:120:3

In order to handle it as a program, writing the same address is easy to manage.

1:ab8:18a3:0:0:8a2e:120:3

 

Code Example

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.regex.Pattern;

public class MyFilterOption {
	public static void main(String[] args) throws UnknownHostException {
		new MyFilterOption().start();
	}

	private static String regexIPv4 = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
	private static String regexIPv6 = "(^((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1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4}:){3}((([0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}((([0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}((([0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))):\\*$)";

	public static boolean checkIpv4OrIpv6(String ipAddr) {
		Pattern pattern;

		pattern = Pattern.compile(regexIPv6);
		if (pattern.matcher(ipAddr).matches() == true) {
			return true;
		}

		pattern = Pattern.compile(regexIPv4);
		if (pattern.matcher(ipAddr).matches() == true) {
			return true;
		}

		return false;
	}

	public void getIpValue(String ipValue) {
		try {
			if (!checkIpv4OrIpv6(ipValue)) {
				throw new UnknownHostException();
			}

			InetAddress inetAddress = InetAddress.getByName(ipValue);
			String shortFormat = inetAddress.getHostAddress();
			System.out.println(shortFormat);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void start() throws UnknownHostException {
		getIpValue("1:ab8:18a3:0000:0000:8a2e:0120:3");
		getIpValue("1:0ab8:18a3:0000:0000:8a2e:120:3");
		getIpValue("1:ab8:18a3:0000:0000:8a2e:120:3");
		getIpValue("1:ab8:18a3::8a2e:120:3");
		getIpValue("1:0ab8:18a3::8a2e:0120:3");
		getIpValue("1:ab8:18a3::8a2e:0120:3");
		getIpValue("1:0ab8:18a3::8a2e:120:3");
		getIpValue("1:ab8:18a3::8a2e:120:3");

		/*
		  1:ab8:18a3:0:0:8a2e:120:3
		 */
	}
}

 

Console Result

1:ab8:18a3:0:0:8a2e:120:3
1:ab8:18a3:0:0:8a2e:120:3
1:ab8:18a3:0:0:8a2e:120:3
1:ab8:18a3:0:0:8a2e:120:3
1:ab8:18a3:0:0:8a2e:120:3
1:ab8:18a3:0:0:8a2e:120:3
1:ab8:18a3:0:0:8a2e:120:3
1:ab8:18a3:0:0:8a2e:120:3

 

Conclusion

N/A

 

728x90
1234···51
반응형

+ Recent posts