验证IP地址的合法性

在开发网络程序时,经常用到IP地址、端口号等信息。为了减少程序出现异常情况,在使用这些信息前,需要先验证其合法性,本实例将演示如何使用正则表达式验证IP地址的合法性。

public class nn {
	public static void main(String[] args)
	{
		String number="((\\d{1,2})|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))";
		String regex="("+number+"\\.){3}"+number;
		String IP="192.168.1.204";
		boolean match=IP.matches(regex);
		System.out.println(IP+"是合法IP:"+match);
	}
}

运行结果如下:


猜你喜欢

转载自blog.csdn.net/qq_39993896/article/details/80570594