1. 程式人生 > 其它 >Java重溫學習筆記,正則式

Java重溫學習筆記,正則式

一、java中正則表示式常用的語法

字元的取值範圍
1.[abc] : 表示可能是a,可能是b,也可能是c。
2.[^abc]: 表示不是a,b,c中的任意一個
3.[a-zA-Z]: 表示是英文字母
4.[0-9]:表示是數字

簡潔的字元表示
.:匹配任意的字元
\d:表示數字
\D:表示非數字
\s:表示由空字元組成,[ \t\n\r\x\f]
\S:表示由非空字元組成,[^\s]
\w:表示字母、數字、下劃線,[a-zA-Z0-9_]
\W:表示不是由字母、數字、下劃線組成

數量表達式
1.?: 表示出現0次或1次
2.+: 表示出現1次或多次
3.*: 表示出現0次、1次或多次
4.{n}:表示出現n次
5.{n,m}:表示出現n~m次
6.{n,}:表示出現n次或n次以上

邏輯表示式
1.XY: 表示X後面跟著Y,這裡X和Y分別是正則表示式的一部分
2.X|Y:表示X或Y,比如"food|f"匹配的是foo(d或f),而"(food)|f"匹配的是food或f
3.(X):子表示式,將X看做是一個整體

二、幾個簡單的正則式示範
1. 表示式t.o 可以匹配:tno,t#o,teo等等。不可以匹配:tnno,to,Tno,t正o等。
2. 表示式:t[abcd]n 只可以匹配:tan,tbn,tcn,tdn。不可以匹配:thn,tabn,tn等。
3. 表示式:t(a|b|c|dd)n 只可以匹配:tan,tbn,tcn,tddn。不可以匹配taan,tn,tabcn等。
4. 表示式:[0—9]{3}\—[0-9]{2}\—[0-9]{3} 的匹配格式為:999—99—999
5. 常用正則表示式
規則 正則表示式語法
一個或多個漢字 ^[\u0391-\uFFE5]+$
郵政編碼 ^[1-9]\d{5}$
QQ號碼 ^[1-9]\d{4,10}$
郵箱 ^[a-zA-Z_]{1,}[0-9]{0,}@(([a-zA-z0-9]-*){1,}\.){1,3}[a-zA-z\-]{1,}$
使用者名稱(字母開頭 + 數字/字母/下劃線) ^[A-Za-z][A-Za-z1-9_-]+$
手機號碼 ^1[3|4|5|8][0-9]\d{8}$
URL ^((http|https)://)?([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$
18位身份證號 ^(\d{6})(18|19|20)?(\d{2})([01]\d)([0123]\d)(\d{3})(\d|X|x)?$


三、由String類已經實現的幾個常用方法
import java.util.Scanner;

public class MyDemo {
    public static void main(String[] args){
        // 1. 判斷功能
        Scanner sc = new Scanner(System.in);
        System.out.println("請輸入手機號碼:");
        String s1 = sc.nextLine();

        String regex1 
= "1[38]\\d{9}"; // 規則:13或18打頭,後面再接9位數字 boolean flag = s1.matches(regex1); System.out.println("手機號正確?" + flag); // 2. 分割功能 String age = "18-24"; String regex2 = "-"; String[] strArr = age.split(regex2); // 分割成字串陣列 // 取得想要的值 int startAge = Integer.parseInt(strArr[0]); int endAge = Integer.parseInt(strArr[1]); // 3. 替換功能 String s3 = "this is my test 1984 hahaha"; String regex3 = "\\d"; String temp = "_"; String result = s3.replaceAll(regex3, temp); System.out.println(result); } }
四、利用Pattern/Matcher處理的示範
import java.util.regex.*;

public class MyDemo {
    public static void main(String[] args) {
        // 1. 檢查郵箱格式是否正確
        String str = "[email protected]";
        String regEx = "[a-zA-Z_]{1,}[0-9]{0,}@(([a-zA-z0-9]-*){1,}\\.){1,3}[a-zA-z\\-]{1,}";

        Pattern pattern = Pattern.compile(regEx);
        // Pattern pattern = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(str);
        boolean result = matcher.matches(); 
        System.out.println(result);
            
        // 2. 從字串中提取出所有數字,並列印起始與結束位置
        str = "我的QQ是:56789 我的電話是:12345 我的郵箱是:[email protected]";
        regEx = "\\d+";
        
        pattern = Pattern.compile(regEx);
        matcher = pattern.matcher(str);
        while(matcher.find()) {
           System.out.println(matcher.group());
           System.out.print("start:" + matcher.start()); 
            System.out.println(" end:" + matcher.end()); 
        }
        
        // 3. 利用String類的方法,將上述字串拆分(去除所有數字)
        String[] myStrArray = str.split(regEx);
        for (String s: myStrArray) {
            System.out.println(s);
        }
    }
}
輸出如下:

%JAVA_HOME%\bin\java "MyDemo" ...
true
56789
start:6 end:11
12345
start:18 end:23
9876
start:34 end:38
163
start:39 end:42
我的QQ是:
我的電話是:
我的郵箱是:test
@
.com



本文參考:
https://www.cnblogs.com/xhj123/p/6032683.html
https://blog.csdn.net/weixin_43860260/article/details/91417485
https://www.cnblogs.com/xyou/p/7427779.html
https://www.cnblogs.com/lzq198754/p/5780340.html