當前位置:才華齋>計算機>java語言>

幫助你駕馭Java正則表示式

java語言 閱讀(1.18W)

  什麼是正則表示式?

幫助你駕馭Java正則表示式

正則表示式定義了字串的模式。正則表示式可以用來搜尋、編輯或處理文字。正則表示式並不僅限於某一種語言,但是在每種語言中有細微的差別。Java正則表示式和Perl的是最為相似的。

Java正則表示式的類在 x 包中,包括三個類:Pattern,Matcher 和 PatternSyntaxException。

Pattern物件是正則表示式的已編譯版本。他沒有任何公共構造器,我們通過傳遞一個正則表示式引數給公共靜態方法 compile 來建立一個pattern物件。

Matcher是用來匹配輸入字串和建立的 pattern 物件的正則引擎物件。這個類沒有任何公共構造器,我們用patten物件的matcher方法,使用輸入字串作為引數來獲得一個Matcher物件。然後使用matches方法,通過返回的布林值判斷輸入字串是否與正則匹配。

如果正則表示式語法不正確將丟擲PatternSyntaxException異常。

讓我們在一個簡單的例子裡看看這些類是怎麼用的吧

package ; import her;import ern; public class RegexExamples { public static void main(String[] args) { // using pattern with flags Pattern pattern = Pattern pile("ab", _INSENSITIVE); Matcher matcher = her("ABcabdAb"); // using Matcher find(), group(), start() and end() methods while (()) { tln("Found the text "" + p() + "" starting at " + t() + " index and ending at index " + ()); } // using Pattern split() method pattern = Pattern pile("W"); String[] words = t("one@two#three:four$five"); for (String s : words) { tln("Split using t(): " + s); } // using aceFirst() and replaceAll() methods pattern = Pattern pile("1*2"); matcher = her("11234512678"); tln("Using replaceAll: " + aceAll("_")); tln("Using replaceFirst: " + aceFirst("_")); } }

既然正則表示式總是和字串有關, Java 1.4對String類進行了擴充套件,提供了一個matches方法來匹配pattern。在方法內部使用Pattern和Matcher類來處理這些東西,但顯然這樣減少了程式碼的行數。

Pattern類同樣有matches方法,可以讓正則和作為引數輸入的字串匹配,輸出布林值結果。

下述的程式碼可以將輸入字串和正則表示式進行匹配。

String str = "bbb";tln("Using String matches method: "+hes(""));tln("Using Pattern matches method: "+hes("", str));

所以如果你的.需要僅僅是檢查輸入字串是否和pattern匹配,你可以通過呼叫String的matches方法省下時間。只有當你需要操作輸入字串或者重用pattern的時候,你才需要使用Pattern和Matches類。

注意由正則定義的pattern是從左至右應用的,一旦一個原字元在一次匹配中使用過了,將不會再次使用。

例如,正則“121”只會匹配兩次字串“31212142121″,就像這樣“_121____121″。

  正則表示式通用匹配符號

Java正則表示式元字元

有兩種方法可以在正則表示式中像一般字元一樣使用元字元。

在元字元前新增反斜槓()

將元字元置於Q(開始引用)和E(結束引用)間

正則表示式量詞

量詞指定了字元匹配的發生次數。

量詞可以和character classes和capturing group一起使用。

例如,[abc]+表示a,b或c出現一次或者多次。

(abc)+表示capturing group “abc”出現一次或多次。我們即將討論capturing group。

正則表示式capturing group

Capturing group是用來對付作為一個整體出現的多個字元。你可以通過使用()來建立一個group。輸入字串中和capturing group相匹配的部分將儲存在記憶體裡,並且可以通過使用Backreference呼叫。

你可以使用pCount方法來獲得一個正則pattern中capturing groups的數目。例如((a)(bc))包含3個capturing groups; ((a)(bc)), (a) 和 (bc)。

你可以使用在正則表示式中使用Backreference,一個反斜槓()接要呼叫的group號碼。

Capturing groups和Backreferences可能很令人困惑,所以我們通過一個例子來理解。

tln(hes("(wd)1", "a2a2")); //true tln(hes("(wd)1", "a2b2")); //false tln(hes("(AB)(Bd)21", "ABB2B2AB")); //true tln(hes("(AB)(Bd)21", "ABB2B3AB")); //false

在第一個例子裡,執行的時候第一個capturing group是(wd),在和輸入字串“a2a2″匹配的時候獲取“a2″並儲存到記憶體裡。因此1是”a2”的引用,並且返回true。基於相同的原因,第二行程式碼列印false。

試著自己理解第三行和第四行程式碼。:)

  現在我們來看看Pattern和Matcher類中一些重要的方法。

我們可以建立一個帶有標誌的Pattern物件。例如_INSENSITIVE可以進行大小寫不敏感的匹配。Pattern類同樣提供了和String類相似的split(String) 方法

Pattern類toString()方法返回被編譯成這個pattern的正則表示式字串。

Matcher類有start()和end()索引方法,他們可以顯示從輸入字串中匹配到的準確位置。

Matcher類同樣提供了字串操作方法replaceAll(String replacement)和replaceFirst(String replacement)。

現在我們在一個簡單的java類中看看這些函式是怎麼用的。

package ; import her;import ern; public class RegexExamples { public static void main(String[] args) { // using pattern with flags Pattern pattern = Pattern pile("ab", _INSENSITIVE); Matcher matcher = her("ABcabdAb"); // using Matcher find(), group(), start() and end() methods while (()) { tln("Found the text "" + p() + "" starting at " + t() + " index and ending at index " + ()); } // using Pattern split() method pattern = Pattern pile("W"); String[] words = t("one@two#three:four$five"); for (String s : words) { tln("Split using t(): " + s); } // using aceFirst() and replaceAll() methods pattern = Pattern pile("1*2"); matcher = her("11234512678"); tln("Using replaceAll: " + aceAll("_")); tln("Using replaceFirst: " + aceFirst("_")); } }

  上述程式的輸出:

Found the text "AB" starting at 0 index and ending at index 2Found the text "ab" starting at 3 index and ending at index 5Found the text "Ab" starting at 6 index and ending at index 8Split using t(): oneSplit using t(): twoSplit using t(): threeSplit using t(): fourSplit using t(): fiveUsing replaceAll: _345_678Using replaceFirst: _34512678