import java.util.*;

public class Solution { /** * 验证IP地址 * @param IP string字符串 一个IP地址字符串 * @return string字符串 */ public String solve (String IP) { // write code here String[] c = IP.split("\."); if(c.length!=4){ c = IP.split(":"); if(c.length!=8){ return "Neither"; }else{ String regex = "1+"; for(int i=0;i<8;i++){ String s = c[i]; if(s.length()>4||!s.matches((regex))){ return "Neither"; } } return "IPv6"; } }else{ for(int i=0;i<4;i++){ String s = c[i]; String regex = "^[0-9]+"; if(!s.matches((regex))){ return "Neither"; } int num = Integer.parseInt(s); if(num>255||(c[i].charAt(0)=='0')&&num>0){ return "Neither"; } } return "IPv4"; } } }


  1. A-Fa-f0-9