Java SE之Regex五:切割

來源:互聯網
上載者:User

標籤:通過   main   data   oid   items   name   public   void   AC   

/** *  * @author Zen Johnny * @date 2018年4月29日 下午3:53:55 * */package demo.regex;/* Regex:切割 */public class RegexSplitDemo {public static void splitNames(String string) {String[] names = string.split("(,|\\s|\\.|;)+");//按照多個空格或者逗號或者分號切割,此處不要用*for(String item:names)System.out.println(item);}//切割檔案目錄public static void splitDirs(String string) {String[] dirs = string.split("\\\\+");for(String item:dirs)System.out.println(item);}public static void namesTest() {splitNames("張三,李四,王五");splitNames("張三     ;李四           ,.;王五");/*張三李四王五張三李四王五*/}public static void dirsTest() {splitDirs("C:\\Desktop\\project\\data.txt");/*C:Desktopprojectdata.txt*/}public static void splitDemo(String string, String regex) {String[] items = string.split(regex);for(String item:items)System.out.println(item);}public static void main(String args[]) {//namesTest();dirsTest();splitDemo("32432443styddxcccccdcfeweertryt", "(.)\\1+");//按照疊詞切割,+號表示前面鄰近的規則執行多次/* 【重點:\1】為了讓規則的結果被重用,可以讓規則封裝成一個組,用()完成。組的出現都有編號:從1開始想要使用已有的組可以通過格式:\ + 組序號Eg:以疊詞作為分隔字元號,"(.)\1"*/}}

  

Java SE之Regex五:切割

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.