溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java正則表達(dá)式的知識(shí)點(diǎn)有哪些

發(fā)布時(shí)間:2022-02-07 14:45:22 來(lái)源:億速云 閱讀:130 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹“java正則表達(dá)式的知識(shí)點(diǎn)有哪些”的相關(guān)知識(shí),小編通過(guò)實(shí)際案例向大家展示操作過(guò)程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“java正則表達(dá)式的知識(shí)點(diǎn)有哪些”文章能幫助大家解決問(wèn)題。

字符
x字符 x
\\反斜線字符
\0n帶有八進(jìn)制值 0 的字符 n (0 <= n <= 7)
\0nn帶有八進(jìn)制值 0 的字符 nn (0 <= n <= 7)
\0mnn帶有八進(jìn)制值 0 的字符 mnn(0 <= m <= 3、0 <= n <= 7)
\xhh帶有十六進(jìn)制值 0x 的字符 hh
\uhhhh帶有十六進(jìn)制值 0x 的字符 hhhh
\t制表符 ('\u0009')
\n新行(換行)符 ('\u000A')
\r回車符 ('\u000D')
\f換頁(yè)符 ('\u000C')
\a報(bào)警 (bell) 符 ('\u0007')
\e轉(zhuǎn)義符 ('\u001B')
\cx對(duì)應(yīng)于 x 的控制符
字符類
[abc]ab 或 c(簡(jiǎn)單類)
[^abc]任何字符,除了 a、b 或 c(否定)
[a-zA-Z]a 到 z 或 A 到 Z,兩頭的字母包括在內(nèi)(范圍)
[a-d[m-p]]a 到 d 或 m 到 p[a-dm-p](并集)
[a-z&&[def]]d、e 或 f(交集)
[a-z&&[^bc]]a 到 z,除了 b 和 c[ad-z](減去)
[a-z&&[^m-p]]a 到 z,而非 m 到 p[a-lq-z](減去)
預(yù)定義字符類
.任何字符(與行結(jié)束符可能匹配也可能不匹配)
\d數(shù)字:[0-9]
\D非數(shù)字: [^0-9]
\s空白字符:[ \t\n\x0B\f\r]
\S非空白字符:[^\s]
\w單詞字符:[a-zA-Z_0-9]
\W非單詞字符:[^\w]
Greedy 數(shù)量詞
X?X,一次或一次也沒(méi)有
X*X,零次或多次
X+X,一次或多次
X{n}X,恰好 n 次
X{n,}X,至少 n 次
X{n,m}X,至少 n 次,但是不超過(guò) m 次
Reluctant 數(shù)量詞
X??X,一次或一次也沒(méi)有
X*?X,零次或多次
X+?X,一次或多次
X{n}?X,恰好 n 次
X{n,}?X,至少 n 次
X{n,m}?X,至少 n 次,但是不超過(guò) m 次

例子 

package com.xiaostudy;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MyPattern {
    public static void main(String[] args) {
    }
    private static void demo_Reluctant() {
        // 檢驗(yàn)規(guī)則,單個(gè)字母,“+”表示:0次或多次,后面多加一個(gè)“?”與不加的區(qū)別是:不加的話表示只匹配一次,加的話表示匹配多次
        String regex = ".+?222";
        // 要檢驗(yàn)的對(duì)象
        String str = "xx222xx222xx222xx222";
        // 編譯正則表達(dá)式
        Pattern pattern = Pattern.compile(regex);
        // 創(chuàng)建匹配器,給定輸入與此模式的匹配
        Matcher matcher = pattern.matcher(str);
        while (matcher.find())
            System.out.println(matcher.start() + "=====" + matcher.end());
        // 匹配,返回結(jié)果
        boolean b = matcher.matches();
        if (b)
            System.out.println(true);
        else
            System.out.println(false);
    }
    private static void demo_aBAb() {
        // 檢驗(yàn)規(guī)則,字母集,“+”表示:0個(gè)或多個(gè)
        String regex = "[abcd]+";
        // 要檢驗(yàn)的對(duì)象
        String str = "adbcdbaDACDBDAC";
        // 編譯正則表達(dá)式,不區(qū)分大小寫(xiě)
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        // 創(chuàng)建匹配器,給定輸入與此模式的匹配
        Matcher matcher = pattern.matcher(str);
        // 匹配,返回結(jié)果
        boolean b = matcher.matches();
        if (b)
            System.out.println(true);
        else
            System.out.println(false);
    }
    private static void demo_abcd() {
        // 檢驗(yàn)規(guī)則,字母集,“+”表示:0個(gè)或多個(gè)
        String regex = "[abcd]+";
        // 要檢驗(yàn)的對(duì)象
        String str = "adbcdabdcddbadbc";
        // 編譯正則表達(dá)式
        Pattern pattern = Pattern.compile(regex);
        // 創(chuàng)建匹配器,給定輸入與此模式的匹配
        Matcher matcher = pattern.matcher(str);
        // 匹配,返回結(jié)果
        boolean b = matcher.matches();
        if (b)
            System.out.println(true);
        else
            System.out.println(false);
    }
    private static void demo_123no() {
        // 檢驗(yàn)規(guī)則,非數(shù)字集,“+”表示:0個(gè)或多個(gè)
        String regex = "[^1-9]+";// 等價(jià)于\\D+
        // 要檢驗(yàn)的對(duì)象
        String str = "+sdoi#$@%@#";
        // 編譯正則表達(dá)式
        Pattern pattern = Pattern.compile(regex);
        // 創(chuàng)建匹配器,給定輸入與此模式的匹配
        Matcher matcher = pattern.matcher(str);
        // 匹配,返回結(jié)果
        boolean b = matcher.matches();
        if (b)
            System.out.println(true);
        else
            System.out.println(false);
    }
    private static void demo_123() {
        // 檢驗(yàn)規(guī)則,數(shù)字集,“+”表示:0個(gè)或多個(gè)
        String regex = "[1-9]+";// 等價(jià)于\\d+
        // 要檢驗(yàn)的對(duì)象
        String str = "123";
        // 編譯正則表達(dá)式
        Pattern pattern = Pattern.compile(regex);
        // 創(chuàng)建匹配器,給定輸入與此模式的匹配
        Matcher matcher = pattern.matcher(str);
        // 匹配,返回結(jié)果
        boolean b = matcher.matches();
        if (b)
            System.out.println(true);
        else
            System.out.println(false);
    }
    private static void demo_2() {
        // 檢驗(yàn)規(guī)則,單個(gè)數(shù)字
        String regex = "[1-9]";
        // 要檢驗(yàn)的對(duì)象
        String str = "2";
        // 編譯正則表達(dá)式
        Pattern pattern = Pattern.compile(regex);
        // 創(chuàng)建匹配器,給定輸入與此模式的匹配
        Matcher matcher = pattern.matcher(str);
        // 匹配,返回結(jié)果
        boolean b = matcher.matches();
        if (b)
            System.out.println(true);
        else
            System.out.println(false);
    }
    private static void demo_nm() {
        // 檢驗(yàn)規(guī)則,單個(gè)字母,“{n,m}”表示:出現(xiàn)n次到m次之間,包括他們本身
        String regex = "x{3,5}";
        // 要檢驗(yàn)的對(duì)象
        String str = "xxxxx";
        // 編譯正則表達(dá)式
        Pattern pattern = Pattern.compile(regex);
        // 創(chuàng)建匹配器,給定輸入與此模式的匹配
        Matcher matcher = pattern.matcher(str);
        // 匹配,返回結(jié)果
        boolean b = matcher.matches();
        if (b)
            System.out.println(true);
        else
            System.out.println(false);
    }
    private static void demo_n0() {
        // 檢驗(yàn)規(guī)則,單個(gè)字母,“{n,}”表示:出現(xiàn)n次或以上
        String regex = "x{3,}";
        // 要檢驗(yàn)的對(duì)象
        String str = "xxxx";
        // 編譯正則表達(dá)式
        Pattern pattern = Pattern.compile(regex);
        // 創(chuàng)建匹配器,給定輸入與此模式的匹配
        Matcher matcher = pattern.matcher(str);
        // 匹配,返回結(jié)果
        boolean b = matcher.matches();
        if (b)
            System.out.println(true);
        else
            System.out.println(false);
    }
    private static void demo_n() {
        // 檢驗(yàn)規(guī)則,單個(gè)字母,“{n}”表示:就出現(xiàn)n次
        String regex = "x{3}";
        // 要檢驗(yàn)的對(duì)象
        String str = "xxx";
        // 編譯正則表達(dá)式
        Pattern pattern = Pattern.compile(regex);
        // 創(chuàng)建匹配器,給定輸入與此模式的匹配
        Matcher matcher = pattern.matcher(str);
        // 匹配,返回結(jié)果
        boolean b = matcher.matches();
        if (b)
            System.out.println(true);
        else
            System.out.println(false);
    }
    private static void demo_xxx0() {
        // 檢驗(yàn)規(guī)則,單個(gè)字母,“+”表示:0次或多次
        String regex = "x+";
        // 要檢驗(yàn)的對(duì)象
        String str = "xxx";
        // 編譯正則表達(dá)式
        Pattern pattern = Pattern.compile(regex);
        // 創(chuàng)建匹配器,給定輸入與此模式的匹配
        Matcher matcher = pattern.matcher(str);
        // 匹配,返回結(jié)果
        boolean b = matcher.matches();
        if (b)
            System.out.println(true);
        else
            System.out.println(false);
    }
    private static void demo_xxx() {
        // 檢驗(yàn)規(guī)則,單個(gè)字母,“*”表示:一次或多次
        String regex = "x*";
        // 要檢驗(yàn)的對(duì)象
        String str = "xxx";
        // 編譯正則表達(dá)式
        Pattern pattern = Pattern.compile(regex);
        // 創(chuàng)建匹配器,給定輸入與此模式的匹配
        Matcher matcher = pattern.matcher(str);
        // 匹配,返回結(jié)果
        boolean b = matcher.matches();
        if (b)
            System.out.println(true);
        else
            System.out.println(false);
    }
    private static void demo_x_01() {
        // 檢驗(yàn)規(guī)則,單個(gè)字母,“?”表示:一次或一次都沒(méi)有
        String regex = "x?";
        // 要檢驗(yàn)的對(duì)象
        String str = "x";
        // 編譯正則表達(dá)式
        Pattern pattern = Pattern.compile(regex);
        // 創(chuàng)建匹配器,給定輸入與此模式的匹配
        Matcher matcher = pattern.matcher(str);
        // 匹配,返回結(jié)果
        boolean b = matcher.matches();
        if (b)
            System.out.println(true);
        else
            System.out.println(false);
    }
    private static void demo_00() {
        // 檢驗(yàn)規(guī)則,單個(gè)字母,“.”表示:任何字符
        String regex = ".";
        // 要檢驗(yàn)的對(duì)象
        String str = "x";
        // 編譯正則表達(dá)式
        Pattern pattern = Pattern.compile(regex);
        // 創(chuàng)建匹配器,給定輸入與此模式的匹配
        Matcher matcher = pattern.matcher(str);
        // 匹配,返回結(jié)果
        boolean b = matcher.matches();
        if (b)
            System.out.println(true);
        else
            System.out.println(false);
    }
    private static void demo_x() {
        // 檢驗(yàn)規(guī)則,單個(gè)字母
        String regex = "x";// 等價(jià)于\\w、[a-z]
        // 要檢驗(yàn)的對(duì)象
        String str = "x";
        // 編譯正則表達(dá)式
        Pattern pattern = Pattern.compile(regex);
        // 創(chuàng)建匹配器,給定輸入與此模式的匹配
        Matcher matcher = pattern.matcher(str);
        // 匹配,返回結(jié)果
        boolean b = matcher.matches();
        if (b)
            System.out.println(true);
        else
            System.out.println(false);
    }
}

關(guān)于“java正則表達(dá)式的知識(shí)點(diǎn)有哪些”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI