java程序进行检查ip地址是否合法

匹配的时候用了正则表达式

import java.util.Scanner;
import java.util.regex.Pattern;

class Client_port {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		String str = scan.next();
		String rex ="((\\d{1,2}|25[0-5]|2[0-4]\\d|1\\d{2})\\.){3}(\\d{1,2}|25[0-5]|2[0-4]\\d|1\\d{2})";// 这里\\d的第一个\是对第二个\的转义,避免某些语言不识别,不加也是课可以的
		          
		Pattern pat = Pattern.compile(rex);// 编译正则表达式
		boolean b = pat.matcher(str).matches();
		if (b) {
			System.out.println("匹配");
		} else {
			System.out.println("不匹配");
		}
	}
}

发布了233 篇原创文章 · 获赞 20 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_42565135/article/details/103008210
今日推荐