Java正則表達(dá)式學(xué)習(xí)教程
本教程旨在幫助你駕馭Java正則表達(dá)式,同時(shí)也幫助我復(fù)習(xí)正則表達(dá)式。
什么是正則表達(dá)式?
正則表達(dá)式定義了字符串的模式。正則表達(dá)式可以用來搜索、編輯或處理文本。正則表達(dá)式并不僅限于某一種語言,但是在每種語言中有細(xì)微的差別。Java正則表達(dá)式和Perl的是最為相似的。
Java正則表達(dá)式的類在 java.util.regex 包中,包括三個(gè)類:Pattern,Matcher 和 PatternSyntaxException。
Pattern對(duì)象是正則表達(dá)式的已編譯版本。他沒有任何公共構(gòu)造器,我們通過傳遞一個(gè)正則表達(dá)式參數(shù)給公共靜態(tài)方法 compile 來創(chuàng)建一個(gè)pattern對(duì)象。
Matcher是用來匹配輸入字符串和創(chuàng)建的 pattern 對(duì)象的正則引擎對(duì)象。這個(gè)類沒有任何公共構(gòu)造器,我們用patten對(duì)象的matcher方法,使用輸入字符串作為參數(shù)來獲得一個(gè)Matcher對(duì)象。然后使用matches方法,通過返回的布爾值判斷輸入字符串是否與正則匹配。
如果正則表達(dá)式語法不正確將拋出PatternSyntaxException異常。
讓我們?cè)谝粋€(gè)簡單的例子里看看這些類是怎么用的吧
package com.journaldev.util; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExamples { public static void main(String[] args) { // using pattern with flags Pattern pattern = Pattern.compile("ab", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher("ABcabdAb"); // using Matcher find(), group(), start() and end() methods while (matcher.find()) { System.out.println("Found the text \"" + matcher.group() + "\" starting at " + matcher.start() + " index and ending at index " + matcher.end()); } // using Pattern split() method pattern = Pattern.compile("\\W"); String[] words = pattern.split("one@two#three:four$five"); for (String s : words) { System.out.println("Split using Pattern.split(): " + s); } // using Matcher.replaceFirst() and replaceAll() methods pattern = Pattern.compile("1*2"); matcher = pattern.matcher("11234512678"); System.out.println("Using replaceAll: " + matcher.replaceAll("_")); System.out.println("Using replaceFirst: " + matcher.replaceFirst("_")); } }
既然正則表達(dá)式總是和字符串有關(guān), Java 1.4對(duì)String類進(jìn)行了擴(kuò)展,提供了一個(gè)matches方法來匹配pattern。在方法內(nèi)部使用Pattern和Matcher類來處理這些東西,但顯然這樣減少了代碼的行數(shù)。
Pattern類同樣有matches方法,可以讓正則和作為參數(shù)輸入的字符串匹配,輸出布爾值結(jié)果。
下述的代碼可以將輸入字符串和正則表達(dá)式進(jìn)行匹配。
String str = "bbb"; System.out.println("Using String matches method: "+str.matches(".bb")); System.out.println("Using Pattern matches method: "+Pattern.matches(".bb", str));
所以如果你的需要僅僅是檢查輸入字符串是否和pattern匹配,你可以通過調(diào)用String的matches方法省下時(shí)間。只有當(dāng)你需要操作輸入字符串或者重用pattern的時(shí)候,你才需要使用Pattern和Matches類。
注意由正則定義的pattern是從左至右應(yīng)用的,一旦一個(gè)原字符在一次匹配中使用過了,將不會(huì)再次使用。
例如,正則“121”只會(huì)匹配兩次字符串“31212142121″,就像這樣“_121____121″。
正則表達(dá)式通用匹配符號(hào)
Java正則表達(dá)式元字符
有兩種方法可以在正則表達(dá)式中像一般字符一樣使用元字符。
在元字符前添加反斜杠(\)
將元字符置于\Q(開始引用)和\E(結(jié)束引用)間
正則表達(dá)式量詞
量詞指定了字符匹配的發(fā)生次數(shù)。
量詞可以和character classes和capturing group一起使用。
例如,[abc]+表示a,b或c出現(xiàn)一次或者多次。
(abc)+表示capturing group “abc”出現(xiàn)一次或多次。我們即將討論capturing group。
正則表達(dá)式capturing group
Capturing group是用來對(duì)付作為一個(gè)整體出現(xiàn)的多個(gè)字符。你可以通過使用()來建立一個(gè)group。輸入字符串中和capturing group相匹配的部分將保存在內(nèi)存里,并且可以通過使用Backreference調(diào)用。
你可以使用matcher.groupCount方法來獲得一個(gè)正則pattern中capturing groups的數(shù)目。例如((a)(bc))包含3個(gè)capturing groups; ((a)(bc)), (a) 和 (bc)。
你可以使用在正則表達(dá)式中使用Backreference,一個(gè)反斜杠(\)接要調(diào)用的group號(hào)碼。
Capturing groups和Backreferences可能很令人困惑,所以我們通過一個(gè)例子來理解。
System.out.println(Pattern.matches("(\\w\\d)\\1", "a2a2")); //true System.out.println(Pattern.matches("(\\w\\d)\\1", "a2b2")); //false System.out.println(Pattern.matches("(AB)(B\\d)\\2\\1", "ABB2B2AB")); //true System.out.println(Pattern.matches("(AB)(B\\d)\\2\\1", "ABB2B3AB")); //false
在第一個(gè)例子里,運(yùn)行的時(shí)候第一個(gè)capturing group是(\w\d),在和輸入字符串“a2a2″匹配的時(shí)候獲取“a2″并保存到內(nèi)存里。因此\1是”a2”的引用,并且返回true?;谙嗤脑?,第二行代碼打印false。
試著自己理解第三行和第四行代碼。:)
現(xiàn)在我們來看看Pattern和Matcher類中一些重要的方法。
我們可以創(chuàng)建一個(gè)帶有標(biāo)志的Pattern對(duì)象。例如Pattern.CASE_INSENSITIVE可以進(jìn)行大小寫不敏感的匹配。Pattern類同樣提供了和String類相似的split(String) 方法
Pattern類toString()方法返回被編譯成這個(gè)pattern的正則表達(dá)式字符串。
Matcher類有start()和end()索引方法,他們可以顯示從輸入字符串中匹配到的準(zhǔn)確位置。
Matcher類同樣提供了字符串操作方法replaceAll(String replacement)和replaceFirst(String replacement)。
現(xiàn)在我們?cè)谝粋€(gè)簡單的java類中看看這些函數(shù)是怎么用的。
package com.journaldev.util; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExamples { public static void main(String[] args) { // using pattern with flags Pattern pattern = Pattern.compile("ab", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher("ABcabdAb"); // using Matcher find(), group(), start() and end() methods while (matcher.find()) { System.out.println("Found the text \"" + matcher.group() + "\" starting at " + matcher.start() + " index and ending at index " + matcher.end()); } // using Pattern split() method pattern = Pattern.compile("\\W"); String[] words = pattern.split("one@two#three:four$five"); for (String s : words) { System.out.println("Split using Pattern.split(): " + s); } // using Matcher.replaceFirst() and replaceAll() methods pattern = Pattern.compile("1*2"); matcher = pattern.matcher("11234512678"); System.out.println("Using replaceAll: " + matcher.replaceAll("_")); System.out.println("Using replaceFirst: " + matcher.replaceFirst("_")); } }
上述程序的輸出:
Found the text "AB" starting at 0 index and ending at index 2 Found the text "ab" starting at 3 index and ending at index 5 Found the text "Ab" starting at 6 index and ending at index 8 Split using Pattern.split(): one Split using Pattern.split(): two Split using Pattern.split(): three Split using Pattern.split(): four Split using Pattern.split(): five Using replaceAll: _345_678 Using replaceFirst: _34512678
這是不是一個(gè)很全面的Java正則表達(dá)式學(xué)習(xí)教程,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
java正則表達(dá)式判斷前端參數(shù)修改表中另一個(gè)字段的值
這篇文章主要介紹了java正則表達(dá)式判斷前端參數(shù)修改表中另一個(gè)字段的值,需要的朋友可以參考下2020-12-12JavaScript+Regex 身份證號(hào)碼的正則表達(dá)式及驗(yàn)證詳解
在做用戶實(shí)名驗(yàn)證時(shí),常會(huì)用到身份證號(hào)碼的正則表達(dá)式及校驗(yàn)方案。本文列舉了兩種驗(yàn)證方案,大家可以根據(jù)自己的項(xiàng)目實(shí)際情況,選擇適合的方案2018-03-03webregexp 正則測(cè)試實(shí)現(xiàn)代碼
WebRegExp 1.0 - 客服果果 [ 無憂版 ]對(duì)于想學(xué)習(xí)正則的朋友是個(gè)不錯(cuò)的在線工具,測(cè)試你正則的正確性。2009-03-03正則表達(dá)式re.sub替換不完整的問題及完整解決方案
re.sub是個(gè)正則表達(dá)式方面的函數(shù),用來實(shí)現(xiàn)通過正則表達(dá)式,實(shí)現(xiàn)比普通字符串的replace更加強(qiáng)大的替換功能。這篇文章主要介紹了正則表達(dá)式re.sub替換不完整的問題及解決方案,需要的朋友可以參考下2018-08-08如何使用正則表達(dá)式對(duì)輸入數(shù)字進(jìn)行匹配詳解
正則表達(dá)式用于字符串處理、表單驗(yàn)證等場(chǎng)合,實(shí)用高效,下面這篇文章主要給大家介紹了關(guān)于如何使用正則表達(dá)式對(duì)輸入數(shù)字進(jìn)行匹配的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10