到底用四種功能中的哪一個呢?或者哪幾個呢?
思路方式:
1.如果只想知道該字元是否對還是錯,使用匹配
2.想要將已有的字串變成另一個字元轉,替換。
3.想要按照自定的方式將字串變成多個字串。切割,擷取規則以外的字串。
4.想要拿到符合要求的字串字串,擷取。
import java.util.*;class RegexText {public static void main(String[] args) {//test_1();//test_2();test_3();} //需求:將下列字串轉成:我要學編程 public static void test_1(){ String str="我我...我我...我要.....要要.....學學.....編程....";str=str.replaceAll("\\.+","");System.out.println(str);str=str.replaceAll("(.)\\1+","$1"); System.out.println(str); } /* 將IP地址進行位址區段順序的排序192.68.1.254 102.49.23.013 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.013 10.10.10.10 2.2.2.2 8.109.90.30";ip=ip.replaceAll("(\\d+)","00$1");System.out.println(ip);ip=ip.replaceAll("0*(\\d{3})","$1"); System.out.println(ip);String[] arr=ip.split(" "); TreeSet<String> ts=new TreeSet<String>();for(String s:arr){ ts.add(s);}for(String s:ts) { System.out.println(s.replaceAll("0*(\\d+)","$1"));} } }