檢查IP地址字符串是否合法

	public static boolean IPCheck(String str) {
		if (str != null && !str.isEmpty()) {
			// 定義正則表達式
			String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
					+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
					+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
			// 判斷ip地址是否與正則表達式匹配
			if (str.matches(regex)) {
				// 返回判斷信息
				return true;
			} else {
				// 返回判斷信息
				return false;
			}
		}
		return false;
	}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章