JAVARegex(三)

來源:互聯網
上載者:User

到底用四種功能中的哪一個呢?或者哪幾個呢?
 思路方式:
 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"));}    } }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.