正則表達式:
正則的使用方便於驗證應用
package Demo_2_4_正則表達式; public class Main { public static void main(String[] args) { String str = "1234"; if (str.matches("\\d+")){ int num = Integer.parseInt(str); System.out.println(num * 2); } } }
1.【數量:單個】字符匹配
- 任意字符:表示由任意字符組成。
public class Main { public static void main(String[] args) { String str = "a"; // 要判斷的數據
String regex = "a"; // 正則表達式,只能匹配單個 a ,不能匹配其它字符或多個 a
System.out.println(str.matches(regex)); } }

- \\ :匹配一個" \ "。
- \n :匹配換行
- \t :匹配制表符
2.【數量:單個】字符集匹配
- [abc] :表示可能是字母a、b、c中的任意一個
public class Main { public static void main(String[] args) { String str = "b"; // 要判斷的數據
String regex = "[abc]"; // 正則表達式
System.out.println(str.matches(regex)); } }

- [^abc]:表示不是a、b、c中的任意一個字母
- [a-zA-Z]:表示由一個任意字母組成,不區分大小寫
public class Main { public static void main(String[] args) { String str = "a"; // 要判斷的數據
String regex = "[a-zA-Z]"; // 正則表達式
System.out.println(str.matches(regex)); } }

- [0-9]:表示數字0至9之間任意一個數字組成
public class Main { public static void main(String[] args) { String str = "1"; // 要判斷的數據 String regex = "[0-9]"; // 正則表達式 System.out.println(str.matches(regex)); } }
3.【數量:單個】簡化的字符集
- " . " :表示任意的一個字符
public class Main { public static void main(String[] args) { String str = "#"; // 要判斷的數據 String regex = "."; // 正則表達式 System.out.println(str.matches(regex)); } }
- \d:等價於[0-9]
- \D:等價於[^0-9]
- \s:匹配任意一位空格,可能是空格、換行、制表符;
public class Main { public static void main(String[] args) { String str = "a\n"; // 要判斷的數據 String regex = "\\D\\s"; // 正則表達式 System.out.println(str.matches(regex)); } }
- \S:匹配任意非空格數據
- \w:匹配字母、數字、下划線,等價於[a-zA-Z_0-9]
- \W:匹配非字母、數字、下划線,等價於[^a-zA-Z_0-9]
4.邊界匹配:
- ^ :匹配邊界開始 -- "^a":匹配a開頭
- $:匹配邊界結束 -- "a$":匹配a結尾
5.數量表達:默認情況下只有加上了數量單位才可以匹配多位字符
- " 表達式? " :表示該正則表達式可以出現0次或1次
- " 表達式* " :表示該正則表達式可以出現0次或多次
- " 表達式+ " :表示該正則表達式可以出現1次或多次
public class Main { public static void main(String[] args) { String str = "aabc"; // 要判斷的數據 String regex = "\\w+"; // 正則表達式 System.out.println(str.matches(regex)); } }
- 表達式{n} :正則表達式的長度正好為n次
- 表達式{n,} :正則表達式的長度為n次以上
- 表達式{n,m}:正則表達式的長度在n~ m次
package Demo_2_4_正則表達式; public class Main { public static void main(String[] args) { String str = "aabc"; // 要判斷的數據 String regex = "\\w{3,}"; // 正則表達式,\\w表示\w:匹配非字母、數字、下划線,等價於[^a-zA-Z_0-9],匹配3次或以上 System.out.println(str.matches(regex)); } }
6. 邏輯表達式:可以連接多個正則
- 表達式X表達式Y:X表達式之后緊跟上Y表達式
public class Main { public static void main(String[] args) { String str = "aabc"; // 要判斷的數據
String regex = "ax"; // 正則表達式,a后面必須跟上x
System.out.println(str.matches(regex)); } }

- 表達式X | 表達式Y:有一個表達式滿足即可
- (表達式):為表達式一個整體描述,可以為整體描述設置數量單位
使用正則匹配:
去掉一串字符串中非字母數字符號:
public class Main { public static void main(String[] args) { String str = "ax12@#qwetquyw12@$#!()qpdoihj2oi23*#(*!498yqHR98@!#¥(*98HR#*(hr3F*(hF#Q$*(HFH''"; // 要判斷的數據
String regex = "[^a-zA-Z0-9]+"; // 正則表達式
System.out.println(str.replaceAll(regex,"")); } }

將字符串按照字母分割:
public class Main { public static void main(String[] args) { String str = "a1111b2222c333d4444e7777"; // 要判斷的數據 String regex = "\\d+"; // 正則表達式 System.out.println(str.replaceAll(regex,"")); for (String s : str.split(regex)) { System.out.println(s); } } }
判斷是否為小數:
public class Main { public static void main(String[] args) { String str = "100.223"; // 要判斷的數據 String regex = "\\d+\\.\\d+"; // 正則表達式 System.out.println(str.matches(regex)); } }
但是這種方式存在缺陷,當判斷為整數或只有小數點的時候,結果為false,所以進行改進為以下代碼:
public class Main { public static void main(String[] args) { String str = "100"; // 要判斷的數據
String regex = "\\d+(\\.\\d+)?"; // 正則表達式
System.out.println(str.matches(regex)); } }

判斷日期型字符串:
import java.text.SimpleDateFormat; public class Main { public static void main(String[] args) throws Exception{ String str = "2020-12-11"; // 要判斷的數據 String regex = "\\d{4}-\\d{2}-\\d{2}"; // 正則表達式 if (str.matches(regex)){ // 符合正則表達式 System.out.println(new SimpleDateFormat("yyy-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}。
匹配郵箱:
package Demo_2_4_正則表達式; import java.text.SimpleDateFormat; public class Main { public static void main(String[] args) throws Exception{ String str = "cdulmjava@cdu.edu.cn"; // 要判斷的數據 String regex = "[a-zA-Z0-9]*.[a-zA-Z]*.[a-zA-Z]*.([a-zA-Z]*)?"; // 正則表達式 String regex1 = "[a-zA-Z0-9]\\w+@[a-zA-Z]+.(cn|com|com.cn|net|gov)+"; // 正則表達式 System.out.println(str.matches(regex1)); } }