package Regex;import java.util.regex.*;import java.util.*;public class RegexGet {public static void main(String args[]){//get();//test_1();test_2();}public static void get(){String str= "good good study,day day up ";String regex ="\\b[a-z]{4}\\b"; //取由四個字元組成的單詞Pattern p = Pattern.compile(regex);//System.out.println(p);Matcher m = p.matcher(str);while(m.find()){System.out.println(m.group()+m.start()+"---"+m.end());}}/* * 練習1 * 將 我我......要要要要要.......學......學學.......編編..........程 轉成 * 我要學編程 * * */public static void test_1(){String str = "我我......要要要要要.......學......學學.......編編..........程";// 先將裡面的點全部替換掉String regex ="\\.+";str=str.replaceAll(regex,"");// 下面將重複出現的字元只保留一個 regex="(.)\\1+"; str=str.replaceAll(regex,"$1");System.out.println(str);}/*練習二 * 對 給出的IP 地址進行排序 * 192.68.1.254 102.49.23.13 10.10.10.10 2.2.2.2. 8.109.90.30 * */public static void test_2(){String ip = "192.68.1.254 102.49.23.13 10.10.10.10 2.2.2.2 8.109.90.30";// 先對IP每一位都添加兩個0String regex = "(\\d+)";ip = ip.replaceAll(regex,"00$1");// 再將IP地址只保留三位regex = "0*(\\d{3})";ip = ip.replaceAll(regex,"$1");// 下面進行切割String[] result = ip.split(" +");Arrays.sort(result);System.out.println("after");for(int i =0;i<result.length;i++)result[i]=result[i].replaceAll("0*(\\d+)","$1"); // 將前面的0 去掉for(String s : result)System.out.println(s);}}