51,412
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) {
IPV4 ipv4 = new IPV4("192.168.0.1");
System.out.println(ipv4.isLegal());
System.out.println(ipv4.getType());
}public class IPV4 {
private String ip;
public IPV4(String ip) {
super();
this.ip = ip;
}
public boolean isLegal() {
if (ip != null && !ip.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 (ip.matches(regex)) {
// 返回判断信息
return true;
} else {
// 返回判断信息
return false;
}
}
return false;
}
public String getType() {
if (isLegal()) {
Integer i = Integer.valueOf(ip.split("\\.")[0]);
if (1 <= i && i <= 126) {
return "A";
} else if (128 <= i && i <= 191) {
return "B";
} else if (192 <= i && i <= 223) {
return "C";
} else {
return "other";
}
}
return "other";
}
public class IPV4 {
private String ip;
public IPV4(String ip) {
super();
this.ip = ip;
}
public boolean isLegal(){
if (ip != null && !ip.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 (ip.matches(regex)) {
// 返回判断信息
return true;
} else {
// 返回判断信息
return false;
}
}
return false;
}
public String getType(){
if (isLegal()) {
Integer i = Integer.valueOf(ip.split("\\.")[0]);
if (1<=i&&i<=126) {
return "A";
}else if (128<=i&&i<=191) {
return "B";
}else if (192<=i&&i<=223) {
return "C";
}else {