java的正則表達(dá)式你知道多少
更新時間:2022年02月07日 11:50:39 作者:xiaostudy
這篇文章主要為大家詳細(xì)介紹了java的正則表達(dá)式,使用表格進(jìn)行介紹,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
| 字符 | |
|---|---|
| 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 | 換頁符 ('\u000C') |
| \a | 報警 (bell) 符 ('\u0007') |
| \e | 轉(zhuǎn)義符 ('\u001B') |
| \cx | 對應(yīng)于 x 的控制符 |
| 字符類 | |
|---|---|
| [abc] | a、b 或 c(簡單類) |
| [^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,一次或一次也沒有 |
| 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() {
// 檢驗(yàn)規(guī)則,單個字母,“+”表示:0次或多次,后面多加一個“?”與不加的區(qū)別是:不加的話表示只匹配一次,加的話表示匹配多次
String regex = ".+?222";
// 要檢驗(yàn)的對象
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個或多個
String regex = "[abcd]+";
// 要檢驗(yàn)的對象
String str = "adbcdbaDACDBDAC";
// 編譯正則表達(dá)式,不區(qū)分大小寫
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個或多個
String regex = "[abcd]+";
// 要檢驗(yàn)的對象
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個或多個
String regex = "[^1-9]+";// 等價于\\D+
// 要檢驗(yàn)的對象
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個或多個
String regex = "[1-9]+";// 等價于\\d+
// 要檢驗(yàn)的對象
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ī)則,單個數(shù)字
String regex = "[1-9]";
// 要檢驗(yàn)的對象
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ī)則,單個字母,“{n,m}”表示:出現(xiàn)n次到m次之間,包括他們本身
String regex = "x{3,5}";
// 要檢驗(yàn)的對象
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ī)則,單個字母,“{n,}”表示:出現(xiàn)n次或以上
String regex = "x{3,}";
// 要檢驗(yàn)的對象
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ī)則,單個字母,“{n}”表示:就出現(xiàn)n次
String regex = "x{3}";
// 要檢驗(yàn)的對象
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ī)則,單個字母,“+”表示:0次或多次
String regex = "x+";
// 要檢驗(yàn)的對象
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ī)則,單個字母,“*”表示:一次或多次
String regex = "x*";
// 要檢驗(yàn)的對象
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ī)則,單個字母,“?”表示:一次或一次都沒有
String regex = "x?";
// 要檢驗(yàn)的對象
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ī)則,單個字母,“.”表示:任何字符
String regex = ".";
// 要檢驗(yàn)的對象
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ī)則,單個字母
String regex = "x";// 等價于\\w、[a-z]
// 要檢驗(yàn)的對象
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);
}
}總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
在Java中Collection的一些常用方法總結(jié)
今天給大家?guī)淼闹R是關(guān)于Java的,文章圍繞著Java中Collection的一些常用方法展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
springmvc接收json串,轉(zhuǎn)換為實(shí)體類List方法
今天小編就為大家分享一篇springmvc接收json串,轉(zhuǎn)換為實(shí)體類List方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
使用javaMail實(shí)現(xiàn)發(fā)送郵件
這篇文章主要為大家詳細(xì)介紹了使用javaMail實(shí)現(xiàn)發(fā)送郵件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-08-08
Java中使用Preconditions來檢查傳入?yún)?shù)介紹
這篇文章主要介紹了Java中使用Preconditions來檢查傳入?yún)?shù)介紹,本文只是作為一個簡單的用法介紹,需要的朋友可以參考下2015-06-06
JAVA設(shè)置手動提交事務(wù),回滾事務(wù),提交事務(wù)的操作
這篇文章主要介紹了JAVA設(shè)置手動提交事務(wù),回滾事務(wù),提交事務(wù)的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Java多線程Future松獲取異步任務(wù)結(jié)果輕松實(shí)現(xiàn)
這篇文章主要為大家介紹了Java多線程Future松獲取異步任務(wù)結(jié)果輕松實(shí)現(xiàn)方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04

