java编写正则表达式,判断给定的是否是一个合法的IP地址怎么写?
2个回答
2022-12-01 · 百度认证:北京惠企网络技术有限公司官方账号
关注
展开全部
正则表达式匹配ip地址,实际上就是分别判断每个点直接的数字是否符合规范。\x0d\x0apackage com;\x0d\x0a import java.util.regex.Matcher;\x0d\x0a import java.util.regex.Pattern; \x0d\x0a public class TestRegex { \x0d\x0a public static boolean isboolIP(String ipAddress){ \x0d\x0a String ip="(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})"; \x0d\x0a Pattern pattern = Pattern.compile(ip); \x0d\x0a Matcher matcher = pattern.matcher(ipAddress); \x0d\x0a return matcher.matches();\x0d\x0a } \x0d\x0a /** * @param args \x0d\x0a */ \x0d\x0a public static void main(String[] args) { \x0d\x0a // TODO Auto-generated method stub \x0d\x0a String ipAddress1 = "10."; \x0d\x0a String ipAddress2 = "0.0.0.0"; \x0d\x0a String ipAddress3 = "255.255.255.255";\x0d\x0a String ipAddress4 = "192.168.2.1";\x0d\x0a String ipAddress5 = "26445687";\x0d\x0a String ipAddress6 = "nihao"; \x0d\x0a String ipAddress7 = "你好!!"; \x0d\x0a if(isboolIP(ipAddress1)){ \x0d\x0a System.out.println("IP正确"); \x0d\x0a }else{ \x0d\x0a System.out.println("IP错误"); \x0d\x0a } if(isboolIP(ipAddress2)){ \x0d\x0a System.out.println("IP正确"); }else{ \x0d\x0a System.out.println("IP错误"); \x0d\x0a } if(isboolIP(ipAddress3)){ \x0d\x0a System.out.println("IP正确"); }else{ \x0d\x0a System.out.println("IP错误"); \x0d\x0a } \x0d\x0a if(isboolIP(ipAddress4)){ \x0d\x0a System.out.println("IP正确"); }else{ \x0d\x0a System.out.println("IP错误");
展开全部
正则表达式匹配ip地址,实际上就是分别判断每个点直接的数字是否符合规范。
package com;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestRegex {
public static boolean isboolIP(String ipAddress){
String ip="(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})";
Pattern pattern = Pattern.compile(ip);
Matcher matcher = pattern.matcher(ipAddress);
return matcher.matches();
}
/** * @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String ipAddress1 = "10.";
String ipAddress2 = "0.0.0.0";
String ipAddress3 = "255.255.255.255";
String ipAddress4 = "192.168.2.1";
String ipAddress5 = "26445687";
String ipAddress6 = "nihao";
String ipAddress7 = "你好!!";
if(isboolIP(ipAddress1)){
System.out.println("IP正确");
}else{
System.out.println("IP错误");
} if(isboolIP(ipAddress2)){
System.out.println("IP正确"); }else{
System.out.println("IP错误");
} if(isboolIP(ipAddress3)){
System.out.println("IP正确"); }else{
System.out.println("IP错误");
}
if(isboolIP(ipAddress4)){
System.out.println("IP正确"); }else{
System.out.println("IP错误");
package com;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestRegex {
public static boolean isboolIP(String ipAddress){
String ip="(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})";
Pattern pattern = Pattern.compile(ip);
Matcher matcher = pattern.matcher(ipAddress);
return matcher.matches();
}
/** * @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String ipAddress1 = "10.";
String ipAddress2 = "0.0.0.0";
String ipAddress3 = "255.255.255.255";
String ipAddress4 = "192.168.2.1";
String ipAddress5 = "26445687";
String ipAddress6 = "nihao";
String ipAddress7 = "你好!!";
if(isboolIP(ipAddress1)){
System.out.println("IP正确");
}else{
System.out.println("IP错误");
} if(isboolIP(ipAddress2)){
System.out.println("IP正确"); }else{
System.out.println("IP错误");
} if(isboolIP(ipAddress3)){
System.out.println("IP正确"); }else{
System.out.println("IP错误");
}
if(isboolIP(ipAddress4)){
System.out.println("IP正确"); }else{
System.out.println("IP错误");
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询