天天看點

String+正則,實作字元串快速調整 | 帶你學《Java語言進階特性》之二十五

上一篇:使用正則必備标記 | 帶你學《Java語言進階特性》之二十四

了解了正規表達式的相關内容,可以發現正規表達式服務于字元串,而Java中String類也針對正規表達式提供了一系列的便捷方法以供調用,讓我們一起來使用正則快速處理字元串吧。

【本節目标】

通過閱讀本節内容,你将接觸到String類針對正規表達式所提供的一系列操作方法,并結合執行個體代碼對其功能有了初步的了解,學會在開發過程中運用這些方法實作字元串的快捷操作。

String類對正則的支援

在進行正規表達式大部分處理的情況下都會基于String類來完成,并且在String類裡面提供有如下與正則有關的操作方法:

No 方法名稱 類型 描述
01 Public boolean matches​(String regex); 普通 将指定字元串進行正則判斷
02 public String replaceAll​(String regex, String replacement); 替換全部
03 public String replaceFirst​(String regex, String replacement); 替換首個
04 public String[] split​(String regex); 正則拆分
05 public String[] split​(String regex, int limit); 正則指定個數拆分

下面通過一些具體範例來對正則的使用進行說明。

範例:實作字元串的替換(删除非字母與數字)

public class JavaAPIDemo {
    public static void main(String[] args) throws Exception {
        String str = "JILO&*()@#$UISD&*(#$HUK34rwyhui*()@#$*()@#$";  //要判斷的資料
        String regex = "[^a-zA-Z0-9]+";  //正規表達式
        System.out.println(str.replaceAll(regex, ""));//JILOUISDHUK34rwyhui
    }
}           

範例:實作字元串的拆分

public class JavaAPIDemo {
    public static void main(String[] args) throws Exception {
        String str = "a1b22c333d4444e55555f666666g";
        String regex = "\\d+";
        String result[] = str.split(regex);
        for (int x = 0 ;x < result.length ; x ++) {
            System.out.println(result[x] + "、");  //a、b、c、d、e、f、g、
        }
    }
}           

在正則處理的時候對于拆分和替換的操作相對容易一些,但是比較麻煩的是資料驗證的部分。

範例:判斷一個資料是否為小數,如果是小數則将其變為double類型

public class JavaAPIDemo {
    public static void main(String[] args) throws Exception {
        String str = "100.32";    //要判斷的資料
        String regex = "\\d+(\\.\\d+)?";  //正規表達式
        System.out.println(str.matches(regex));  
    }
}           

範例:判斷一個字元串是否由日期所組成,如果是由日期所組成則将其轉為Date類型

public class JavaAPIDemo {
    public static void main(String[] args) throws Exception {
        String str = "1981-10-15 ";    //要判斷的資料
        String regex = "\\d{4}-\\d{2}-\\d{2}";   //正規表達式
        if (str.matches(regex)) {
            System.out.println(new SimpleDateFormat("yyyy-MM-dd").parse(str));
        } 
    }
}           

需要注意的是,正規表達式無法對裡面的内容進行判斷,隻能夠對格式進行判斷處理。

範例:判斷給定的号碼是否正确

  • 電話号碼:51283346、\\d{7,8};
  • 電話号碼:01051283346、(\\d{3,4})?\\d{7,8};
  • 電話号碼:(010)-51283346、((\\d{3,4})|(\\(\\d{3,4}\\)-))?\\d{7,8};
public class JavaAPIDemo {
    public static void main(String[] args) throws Exception {
        String strA = "51283346";
        String strB = "01051283346";
        String strC = "(010)-51283346";
        String regex = "((\\d{3,4})|(\\(\\d{3,4}\\)-))?\\d{7,8}";//正規表達式
        System.out.println(strA.matches(regex));
        System.out.println(strB.matches(regex));
        System.out.println(strC.matches(regex));  //true
    }
}           

既然已經可以使用正則進行驗證了,那麼下面就可以利用其來實作一個email位址格式的驗證。

範例:驗證email格式

  • email的使用者名可以由字母、數字、_所組成(不應該使用“_”開頭);
  • email的域名可以由字母、數字、_、-所組成;
  • email的域名字尾必須是:.cn、.com、.net、.com.cn、.gov;
String+正則,實作字元串快速調整 | 帶你學《Java語言進階特性》之二十五

郵箱正則分析

public class JavaAPIDemo {
    public static void main(String[] args) throws Exception {
        String str="[email protected]";
        String regex="[a-zA-Z0-9]\\w+@\\w+\\.(cn|com|net|com\\.cn|gov)";
        System.out.println(str.matches(regex));
    }
}           

 以上這幾種比對處理操作是最常用的幾種處理形式。

想學習更多的Java的課程嗎?從小白到大神,從入門到精通,更多精彩不容錯過!免費為您提供更多的學習資源。

本内容視訊來源于

阿裡雲大學 下一篇:借助regex包完成正則進階操作 | 帶你學《Java語言進階特性》之二十六 更多Java面向對象程式設計文章檢視此處