在Java中使用基本的正則表達式
更新時間:2021年07月01日 08:55:27 作者:水墨之白
這篇文章主要介紹了在Java中使用基本的正則表達式,本文通過簡要的案例,說明了很多場景下的正則表達式的用法,列出了正則表達式匹配規(guī)則的表格,需要的朋友可以參考下
一、正則表達式簡介
正則表達式是使用單個字符串來描述、匹配一系列匹配某個句法規(guī)則的字符串。爬蟲中解析html可以使用正則來方便的提取信息
二、正則表達式匹配規(guī)則
| 模式 | 描述 |
|---|---|
| \w | 匹配字母、數(shù)字、下劃線 |
| \W | 匹配非字母、數(shù)字、下劃線 |
| \s | 匹配任意空白字符,相當(dāng)于[\t\n\r\f] |
| \S | 匹配任意非空字符 |
| \d | 匹配任意數(shù)字,相當(dāng)于[0-9] |
| \D | 匹配非數(shù)字的字符 |
| \A | 匹配字符串開頭 |
| \Z | 匹配字符串結(jié)尾,如果存在換行,只匹配到換行前的結(jié)束字符串 |
| \z | 匹配字符串結(jié)尾,如果存在換行,同時還會匹配換行符 |
| \G | 匹配最后匹配完成的位置 |
| \n | 匹配一個換行符 |
| \t | 匹配一個制表符 |
| ^ | 匹配一行字符串的開頭 |
| $ | 匹配一行字符串的結(jié)尾 |
| . | 匹配任意字符,除了換行符 |
| [^…] | 不在[]中的字符,比如[^abc]匹配除了a、b、c之外的字符 |
| * | 匹配0個或多個表達式 |
| + | 匹配1個或多個表達式 |
| ? | 匹配0個或1個前面的正則表達式定義的片段,非貪婪方式 |
| () | 匹配括號內(nèi)的表達式,也表示一個組 |
| {n} | 精確匹配n個前面的表達式,比如\d{n},代表n個數(shù)字 |
| {n,m} | 匹配n到m次由前面正則表達式定義的片段,貪婪方式 |
代碼實戰(zhàn):
public class RegexAction {
public static void main(String[] args) {
String s = "Hello 123 4567 World_This is a Regex Demo";
//match_1(s);
//match_2(s);
//match_3(s);
//match_4(s);
//match_5(s);
match_6(s);
}
private static void match_1(String s) {
Pattern pattern = Pattern.compile("^Hello\\s\\d\\d\\d\\s\\d{4}\\s\\w{10}");
Matcher matcher = pattern.matcher(s);
if(matcher.find()) {
System.out.println(matcher.group(0));
}
}
private static void match_2(String s) {
Pattern pattern = Pattern.compile("Hello\\s(\\d+)\\s\\d{4}\\s\\w{10}");
Matcher matcher = pattern.matcher(s);
if(matcher.find()) {
System.out.println(matcher.group(0)); //匹配到的整個結(jié)果
System.out.println(matcher.group(1)); //匹配到的第一個括號中的結(jié)果
}
}
private static void match_3(String s) {
Pattern pattern = Pattern.compile("Hello\\s(\\d*)\\s\\d{4}\\s\\w{10}");
Matcher matcher = pattern.matcher(s);
if(matcher.find()) {
System.out.println(matcher.group(0)); //匹配到的整個結(jié)果
System.out.println(matcher.group(1)); //匹配到的第一個括號中的結(jié)果
}
}
private static void match_4(String s) {
Pattern pattern = Pattern.compile("Hello.*Demo");
Matcher matcher = pattern.matcher(s);
if(matcher.find()) {
System.out.println(matcher.group(0)); //匹配到的整個結(jié)果
}
}
/**
* 貪婪匹配
* 匹配中間數(shù)字,只能得到7
* .*會盡可能多的匹配數(shù)據(jù)
* @param s
*/
private static void match_5(String s) {
Pattern pattern = Pattern.compile("Hello.*(\\d+).*Demo");
Matcher matcher = pattern.matcher(s);
if(matcher.find()) {
System.out.println(matcher.group(1)); //匹配到的整個結(jié)果
}
}
/**
* .*?非貪婪匹配
* @param s
*/
private static void match_6(String s) {
Pattern pattern = Pattern.compile("Hello.*?(\\d+).*Demo");
Matcher matcher = pattern.matcher(s);
if(matcher.find()) {
System.out.println(matcher.group());
System.out.println(matcher.group(1));
}
}
/**
* 正則表達式字符串也可以不編譯直接使用
* @param s
*/
private static void match_7(String s) {
String regex = "Hello.*?(\\d+).*Demo";
boolean flag = s.matches(regex);
System.out.println(flag);
}
}
- Pattern 類
pattern 對象是一個正則表達式的編譯表示 - Matcher 類
Matcher 對象是對輸入字符串進行解釋和匹配操作的引擎 - find()方法
嘗試查找與該模式匹配的輸入序列的下一個子序列,直到搜索到輸入序列結(jié)束
可以從指定位置開始匹配find(int start)
到此這篇關(guān)于在Java中使用基本的正則表達式的文章就介紹到這了,更多相關(guān)Java使用正則表達式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot的統(tǒng)一異常處理,使用@RestControllerAdvice詳解
@RestControllerAdvice是Spring Boot中的全局異常處理注解,結(jié)合了@ControllerAdvice和@ResponseBody的功能,通過創(chuàng)建自定義異常類和全局異常處理器,可以實現(xiàn)統(tǒng)一異常處理,確保API的一致性和響應(yīng)的標準化2024-12-12
解決SpringBoot連接SqlServer出現(xiàn)的問題
在嘗試通過SSL與SQL?Server建立安全連接時,如果遇到“PKIX?path?building?failed”錯誤,可能是因為未能正確配置或信任服務(wù)器證書,當(dāng)"Encrypt"屬性設(shè)置為"true"且"trustServerCertificate"屬性設(shè)置為"false"時,要求驅(qū)動程序使用安全套接字層(SSL)加密與SQL?Server建立連接2024-10-10
SpringBoot+BootStrap多文件上傳到本地實例
這篇文章主要介紹了SpringBoot+BootStrap多文件上傳到本地實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Spring?@Cacheable注解類內(nèi)部調(diào)用失效的解決方案
這篇文章主要介紹了Spring?@Cacheable注解類內(nèi)部調(diào)用失效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01

