import java.util.Arrays;import java.util.Comparator;import java.util.Scanner;import java.util.regex.Matcher;import java.util.regex.Pattern;class MyComparator implements Comparator{ public int compare(String ip1, String ip2) { Pattern p = Pattern.compile("\\d{1,3}"); Matcher m1 = p.matcher(ip1); Matcher m2 = p.matcher(ip2); while(m1.find()){ m2.find(); System.out.println(m1.group() + " " + m2.group()); int i1 = Integer.valueOf(m1.group()); int i2 = Integer.valueOf(m2.group()); if(i1 < i2) return -1; else if(i1 > i2) return 1; } return 0; } }public class RegexDemo { /** * @param args */ public static void main(String[] args) { /* * 治疗口吃:"我我..我我....我我要要...要要..要要要要学学学...学学学学编编编...编编编编程程程..程..程程...程.程程" * 变成“我要学编程” * 1.替换 2.处理叠词 */ String str="我我..我我....我我要要...要要..要要要要学学学...学学学学编编编...编编编编程程程..程..程程...程.程程"; str = str.replaceAll("\\.+", ""); str = str.replaceAll("(.)\\1+", "$1"); System.out.println(str); /* * ip地址的排序: * 192.168.10.34 127.0.0.1 3.3.3。3 105.70.11.55 * 对应段的数字比较! * */ //第一种方法,获取每一个字段的数字,数字之间进行比较 String[] ip = {"192.168.10.34", "127.0.0.1", "3.3.3.3", "105.70.11.55"}; Arrays.sort(ip, new MyComparator()); for(int i=0; i