java的正則表達式你知道多少
更新時間:2022年02月07日 11:50:39 作者:xiaostudy
這篇文章主要為大家詳細介紹了java的正則表達式,使用表格進行介紹,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
字符 | |
---|---|
x | 字符 x |
\\ | 反斜線字符 |
\0n | 帶有八進制值 0 的字符 n (0 <= n <= 7) |
\0nn | 帶有八進制值 0 的字符 nn (0 <= n <= 7) |
\0mnn | 帶有八進制值 0 的字符 mnn(0 <= m <= 3、0 <= n <= 7) |
\xhh | 帶有十六進制值 0x 的字符 hh |
\uhhhh | 帶有十六進制值 0x 的字符 hhhh |
\t | 制表符 ('\u0009') |
\n | 新行(換行)符 ('\u000A') |
\r | 回車符 ('\u000D') |
\f | 換頁符 ('\u000C') |
\a | 報警 (bell) 符 ('\u0007') |
\e | 轉義符 ('\u001B') |
\cx | 對應于 x 的控制符 |
字符類 | |
---|---|
[abc] | a、b 或 c(簡單類) |
[^abc] | 任何字符,除了 a、b 或 c(否定) |
[a-zA-Z] | a 到 z 或 A 到 Z,兩頭的字母包括在內(范圍) |
[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](減去) |
預定義字符類 | |
---|---|
. | 任何字符(與行結束符可能匹配也可能不匹配) |
\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,一次或一次也沒有 |
X* | X,零次或多次 |
X+ | X,一次或多次 |
X{n} | X,恰好 n 次 |
X{n,} | X,至少 n 次 |
X{n,m} | X,至少 n 次,但是不超過 m 次 |
Reluctant 數(shù)量詞 | |
---|---|
X?? | X,一次或一次也沒有 |
X*? | X,零次或多次 |
X+? | X,一次或多次 |
X{n}? | X,恰好 n 次 |
X{n,}? | X,至少 n 次 |
X{n,m}? | X,至少 n 次,但是不超過 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() { // 檢驗規(guī)則,單個字母,“+”表示:0次或多次,后面多加一個“?”與不加的區(qū)別是:不加的話表示只匹配一次,加的話表示匹配多次 String regex = ".+?222"; // 要檢驗的對象 String str = "xx222xx222xx222xx222"; // 編譯正則表達式 Pattern pattern = Pattern.compile(regex); // 創(chuàng)建匹配器,給定輸入與此模式的匹配 Matcher matcher = pattern.matcher(str); while (matcher.find()) System.out.println(matcher.start() + "=====" + matcher.end()); // 匹配,返回結果 boolean b = matcher.matches(); if (b) System.out.println(true); else System.out.println(false); } private static void demo_aBAb() { // 檢驗規(guī)則,字母集,“+”表示:0個或多個 String regex = "[abcd]+"; // 要檢驗的對象 String str = "adbcdbaDACDBDAC"; // 編譯正則表達式,不區(qū)分大小寫 Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); // 創(chuàng)建匹配器,給定輸入與此模式的匹配 Matcher matcher = pattern.matcher(str); // 匹配,返回結果 boolean b = matcher.matches(); if (b) System.out.println(true); else System.out.println(false); } private static void demo_abcd() { // 檢驗規(guī)則,字母集,“+”表示:0個或多個 String regex = "[abcd]+"; // 要檢驗的對象 String str = "adbcdabdcddbadbc"; // 編譯正則表達式 Pattern pattern = Pattern.compile(regex); // 創(chuàng)建匹配器,給定輸入與此模式的匹配 Matcher matcher = pattern.matcher(str); // 匹配,返回結果 boolean b = matcher.matches(); if (b) System.out.println(true); else System.out.println(false); } private static void demo_123no() { // 檢驗規(guī)則,非數(shù)字集,“+”表示:0個或多個 String regex = "[^1-9]+";// 等價于\\D+ // 要檢驗的對象 String str = "+sdoi#$@%@#"; // 編譯正則表達式 Pattern pattern = Pattern.compile(regex); // 創(chuàng)建匹配器,給定輸入與此模式的匹配 Matcher matcher = pattern.matcher(str); // 匹配,返回結果 boolean b = matcher.matches(); if (b) System.out.println(true); else System.out.println(false); } private static void demo_123() { // 檢驗規(guī)則,數(shù)字集,“+”表示:0個或多個 String regex = "[1-9]+";// 等價于\\d+ // 要檢驗的對象 String str = "123"; // 編譯正則表達式 Pattern pattern = Pattern.compile(regex); // 創(chuàng)建匹配器,給定輸入與此模式的匹配 Matcher matcher = pattern.matcher(str); // 匹配,返回結果 boolean b = matcher.matches(); if (b) System.out.println(true); else System.out.println(false); } private static void demo_2() { // 檢驗規(guī)則,單個數(shù)字 String regex = "[1-9]"; // 要檢驗的對象 String str = "2"; // 編譯正則表達式 Pattern pattern = Pattern.compile(regex); // 創(chuàng)建匹配器,給定輸入與此模式的匹配 Matcher matcher = pattern.matcher(str); // 匹配,返回結果 boolean b = matcher.matches(); if (b) System.out.println(true); else System.out.println(false); } private static void demo_nm() { // 檢驗規(guī)則,單個字母,“{n,m}”表示:出現(xiàn)n次到m次之間,包括他們本身 String regex = "x{3,5}"; // 要檢驗的對象 String str = "xxxxx"; // 編譯正則表達式 Pattern pattern = Pattern.compile(regex); // 創(chuàng)建匹配器,給定輸入與此模式的匹配 Matcher matcher = pattern.matcher(str); // 匹配,返回結果 boolean b = matcher.matches(); if (b) System.out.println(true); else System.out.println(false); } private static void demo_n0() { // 檢驗規(guī)則,單個字母,“{n,}”表示:出現(xiàn)n次或以上 String regex = "x{3,}"; // 要檢驗的對象 String str = "xxxx"; // 編譯正則表達式 Pattern pattern = Pattern.compile(regex); // 創(chuàng)建匹配器,給定輸入與此模式的匹配 Matcher matcher = pattern.matcher(str); // 匹配,返回結果 boolean b = matcher.matches(); if (b) System.out.println(true); else System.out.println(false); } private static void demo_n() { // 檢驗規(guī)則,單個字母,“{n}”表示:就出現(xiàn)n次 String regex = "x{3}"; // 要檢驗的對象 String str = "xxx"; // 編譯正則表達式 Pattern pattern = Pattern.compile(regex); // 創(chuàng)建匹配器,給定輸入與此模式的匹配 Matcher matcher = pattern.matcher(str); // 匹配,返回結果 boolean b = matcher.matches(); if (b) System.out.println(true); else System.out.println(false); } private static void demo_xxx0() { // 檢驗規(guī)則,單個字母,“+”表示:0次或多次 String regex = "x+"; // 要檢驗的對象 String str = "xxx"; // 編譯正則表達式 Pattern pattern = Pattern.compile(regex); // 創(chuàng)建匹配器,給定輸入與此模式的匹配 Matcher matcher = pattern.matcher(str); // 匹配,返回結果 boolean b = matcher.matches(); if (b) System.out.println(true); else System.out.println(false); } private static void demo_xxx() { // 檢驗規(guī)則,單個字母,“*”表示:一次或多次 String regex = "x*"; // 要檢驗的對象 String str = "xxx"; // 編譯正則表達式 Pattern pattern = Pattern.compile(regex); // 創(chuàng)建匹配器,給定輸入與此模式的匹配 Matcher matcher = pattern.matcher(str); // 匹配,返回結果 boolean b = matcher.matches(); if (b) System.out.println(true); else System.out.println(false); } private static void demo_x_01() { // 檢驗規(guī)則,單個字母,“?”表示:一次或一次都沒有 String regex = "x?"; // 要檢驗的對象 String str = "x"; // 編譯正則表達式 Pattern pattern = Pattern.compile(regex); // 創(chuàng)建匹配器,給定輸入與此模式的匹配 Matcher matcher = pattern.matcher(str); // 匹配,返回結果 boolean b = matcher.matches(); if (b) System.out.println(true); else System.out.println(false); } private static void demo_00() { // 檢驗規(guī)則,單個字母,“.”表示:任何字符 String regex = "."; // 要檢驗的對象 String str = "x"; // 編譯正則表達式 Pattern pattern = Pattern.compile(regex); // 創(chuàng)建匹配器,給定輸入與此模式的匹配 Matcher matcher = pattern.matcher(str); // 匹配,返回結果 boolean b = matcher.matches(); if (b) System.out.println(true); else System.out.println(false); } private static void demo_x() { // 檢驗規(guī)則,單個字母 String regex = "x";// 等價于\\w、[a-z] // 要檢驗的對象 String str = "x"; // 編譯正則表達式 Pattern pattern = Pattern.compile(regex); // 創(chuàng)建匹配器,給定輸入與此模式的匹配 Matcher matcher = pattern.matcher(str); // 匹配,返回結果 boolean b = matcher.matches(); if (b) System.out.println(true); else System.out.println(false); } }
總結
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內容!
相關文章
關于@RequestBody和@RequestParam注解的使用詳解
這篇文章主要介紹了關于@RequestBody和@RequestParam注解的使用詳解,本文十分具有參考意義,希望可以幫助到你,如果有錯誤的地方還望不吝賜教2023-03-03Java程序中使用JavaMail發(fā)送帶圖片和附件的郵件
這篇文章主要介紹了Java程序中使用JavaMail發(fā)送帶圖片和附件的郵件,JavaMail是專門用來處理郵件的Java API,需要的朋友可以參考下2015-11-11Java實現(xiàn)撲克牌的創(chuàng)建以及發(fā)放
在java當中生成一副牌有很多種方法,有簡單易于理解的面向過程編程,也有面向對象模塊化編程,下面這篇文章主要給大家介紹了關于Java實現(xiàn)撲克牌的創(chuàng)建以及發(fā)放的相關資料,需要的朋友可以參考下2023-03-03詳解PowerDesigner之CDM、PDM、SQL之間轉換
這篇文章主要介紹了詳解PowerDesigner之CDM、PDM、SQL之間轉換的相關資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-10-10SpringBoot中使用Filter和Interceptor的示例代碼
這篇文章主要介紹了SpringBoot中使用Filter和Interceptor的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-06-06SpringBoot用多線程批量導入數(shù)據(jù)庫實現(xiàn)方法
這篇文章主要介紹了SpringBoot用多線程批量導入數(shù)據(jù)庫實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2023-02-02基于parameters參數(shù)實現(xiàn)參數(shù)化過程解析
這篇文章主要介紹了基于parameters參數(shù)實現(xiàn)參數(shù)化過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-08-08SpringBoot同一接口多個實現(xiàn)類配置的實例詳解
這篇文章主要介紹了SpringBoot同一接口多個實現(xiàn)類配置的實例詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11