扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
没必要判断的那么细,用正则表达式
目前创新互联公司已为成百上千家的企业提供了网站建设、域名、网页空间、网站托管、企业网站设计、阳东网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
public class $ {
public static void main(String[] args) {
String str1 = "a.b.c.d";
String str2 = "1.1.1.1";
String str3 = "1.1.1.256";
String str4 = "192.168.1.1";
String regex = "(2[5][0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})";
System.out.println(str1.matches(regex));
System.out.println(str2.matches(regex));
System.out.println(str3.matches(regex));
System.out.println(str4.matches(regex));
}
}
false
true
false
true
/*Java 验证Ip是否合法*/
public static boolean isIPAddress(String ipaddr) {
boolean flag = false;
Pattern pattern = Pattern.compile("\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b");
Matcher m = pattern.matcher(ipaddr);
flag = m.matches();
return flag;
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class IpTest {
public static void main(String[] args) {
new IpTest().go();
}
/**
* 程序主要逻辑
*/
public void go() {
String IP = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
IP = br.readLine();
} catch (IOException ex) {
ex.printStackTrace();
}
//String IP="192.168.0.100/255.255.255.0";
IP = IP.replace(" ", "");
String[] temp = IP.split("/");
String[] sIP = temp[0].split("\\.");
/**
* 判断输入格式是否正确
*/
if (temp.length 2) {
System.out.println("对不起,输入格式错误");
System.exit(0);
}
int Mask = Integer.parseInt(temp[1]);
int[] ip = new int[4];
for (int i = 0; i sIP.length; i++) {
ip[i] = Integer.parseInt(sIP[i]);
}
/**
* IP地址各段不能大于255
*/
for (int i = 0; i 4; i++) {
if (ip[i] 255) {
System.out.println("对不起,IP输入错误");
System.exit(0);
}
}
/**
* 判断IP地址所属分类
*/
int ipclass = 0;
if (ip[0] 127) {
System.out.println("A类地址");
ipclass = 1;
} else if (ip[0] 192) {
System.out.println("B类地址");
ipclass = 2;
} else if (ip[0] 224) {
System.out.println("C类地址");
ipclass = 3;
}
/**
* 判断子网掩码是否输入正确
*/
if (Mask 8 * ipclass) {
System.out.println("对不起,子网掩码输入错误");
System.exit(0);
}
int b = 0;
int a = Mask % 8;
b = Mask / 8;
StringBuffer sb = new StringBuffer();
for (int i = 0; i a; i++)
sb.append('1');
for (int i = 0; i 8 - a; i++)
sb.append('0');
int end = Integer.parseInt(sb.toString(), 2);
ip[b] = ip[b] end;
for (int i = b + 1; i 4; i++)
ip[i] = 0;
for (int i = 0; i 4; i++) {
System.out.print(ip[i]);
if (i != 3)
System.out.print(".");
}
}
}
public static void main(String[] args) {
String myIP = "160.1.1.1";
boolean state = isIp(myIP);
System.out.println(state);
}
public static boolean isIp(String ip){//判断是否是一个IP
boolean b = false;
ip = trimSpaces(ip);
if(ip.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")){
String s[] = ip.split("\\.");
if(Integer.parseInt(s[0])255)
if(Integer.parseInt(s[1])255)
if(Integer.parseInt(s[2])255)
if(Integer.parseInt(s[3])255)
b = true;
}
return b;
}
public static String trimSpaces(String ip){//去掉IP字符串前后所有的空格
while(ip.startsWith(" ")){
ip= ip.substring(1,ip.length()).trim();
}
while(ip.endsWith(" ")){
ip= ip.substring(0,ip.length()-1).trim();
}
return ip;
}
希望能帮到你!
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流