发布网友 发布时间:2022-04-29 18:13
共3个回答
热心网友 时间:2023-11-20 16:00
正则表达式匹配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错误");热心网友 时间:2023-10-29 15:02
正则表达式匹配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错误");热心网友 时间:2023-10-29 15:03
正则表达式匹配ip地址,实际上就是分别判断每个点直接的数字是否符合规范。热心网友 时间:2023-10-29 15:03
^[\d]{1,3}.{1}[\d]{1,3}.{1}[\d]{1,3}.{1}[\d]{1,3}$热心网友 时间:2023-11-20 16:00
正则表达式匹配ip地址,实际上就是分别判断每个点直接的数字是否符合规范。热心网友 时间:2023-11-20 16:01
^[\d]{1,3}.{1}[\d]{1,3}.{1}[\d]{1,3}.{1}[\d]{1,3}$热心网友 时间:2023-11-20 16:00
正则表达式匹配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错误");热心网友 时间:2023-11-20 16:00
正则表达式匹配ip地址,实际上就是分别判断每个点直接的数字是否符合规范。热心网友 时间:2023-11-20 16:01
^[\d]{1,3}.{1}[\d]{1,3}.{1}[\d]{1,3}.{1}[\d]{1,3}$