private static void IPsort(String str) { //1,在所有数字前加两个0 str=str.replaceAll("(\w+)", "00$1"); //2,保留三位数字,倒数 str=str.replaceAll("0*(\w{3})", "$1"); //3,按空格分开 String[] str1=str.split(" +"); Arrays.sort(str1); for(String ip:str1){//输出去掉0 System.out.println(ip.replaceAll("0*(\w+)", "$1")); } }