- package com.itcast.regex;
- import java.util.TreeSet;
- public class IpRegexPractice {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- ipSort("192.168.1.154 196.154.01.002 10.10.10.10 2.2.2.2 198.40.5.4 187.255.255.255");
- }
-
- public static void ipSort(String ip){
-
- //都视为一位数字,前面先加上00
- ip=ip.replaceAll("(\\d+)", "00$1");
- System.out.println(ip);
-
- //去掉数字前面多余的0
- ip=ip.replaceAll("0*(\\d+{3})","$1");
- System.out.println(ip);
-
- //获取地址值
- String[]arr=ip.split(" +");
- TreeSet<String> ts=new TreeSet<String>();
-
- for (String string : arr) {
- ts.add(string);
- }
-
- for (String string : ts) {
- System.out.println(string.replaceAll("0*(\\d+)","$1"));
- }
- }
- }
复制代码
网速太慢,代码貌似没传上去, |