溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java正則表達式API邊界匹配怎么實現(xiàn)

發(fā)布時間:2022-06-10 09:22:09 來源:億速云 閱讀:174 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“Java正則表達式API邊界匹配怎么實現(xiàn)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“Java正則表達式API邊界匹配怎么實現(xiàn)”吧!

Boundary Matchers

Java regex API還支持邊界匹配。如果我們關(guān)心在輸入文本中匹配的確切位置,那么這就是我們要尋找的。在前面的示例中,我們關(guān)心的只是是否找到匹配項。

為了僅在文本開頭所需的正則表達式為true時匹配,我們使用插入符號^。

此測試將失敗,因為可以在開頭找到文本dog:

@Test
public void givenText_whenMatchesAtBeginning_thenCorrect() {
    int matches = runTest("^dog", "dogs are friendly");
 
    assertTrue(matches > 0);
}

下面的測試將失敗:

@Test
public void givenTextAndWrongInput_whenMatchFailsAtBeginning_
  thenCorrect() {
    int matches = runTest("^dog", "are dogs are friendly?");
 
    assertFalse(matches > 0);
}

為了僅在文本末尾所需的正則表達式為true時匹配,我們使用美元字符$。在以下情況下會找到匹配項:

@Test
public void givenText_whenMatchesAtEnd_thenCorrect() {
    int matches = runTest("dog$", "Man's best friend is a dog");
 
    assertTrue(matches > 0);
}

并且沒有找到匹配:

@Test
public void givenTextAndWrongInput_whenMatchFailsAtEnd_thenCorrect() {
    int matches = runTest("dog$", "is a dog man's best friend?");
 
    assertFalse(matches > 0);
}

如果僅在單詞邊界處找到所需文本時才需要匹配,則在正則表達式的開頭和結(jié)尾使用\\b正則表達式:

空格是單詞邊界:

@Test
public void givenText_whenMatchesAtWordBoundary_thenCorrect() {
    int matches = runTest("\\bdog\\b", "a dog is friendly");
 
    assertTrue(matches > 0);
}

行首的空字符串也是單詞邊界:

@Test
public void givenText_whenMatchesAtWordBoundary_thenCorrect2() {
    int matches = runTest("\\bdog\\b", "dog is man's best friend");
 
    assertTrue(matches > 0);
}

這些測試之所以通過,是因為字符串的開頭以及文本之間的空格標記了單詞邊界,但是以下測試顯示了相反的結(jié)果:

@Test
public void givenWrongText_whenMatchFailsAtWordBoundary_thenCorrect() {
    int matches = runTest("\\bdog\\b", "snoop dogg is a rapper");
 
    assertFalse(matches > 0);
}

一行中出現(xiàn)的兩個單詞字符不會標記單詞邊界,但我們可以通過更改正則表達式的結(jié)尾來查找非單詞邊界:

@Test
public void givenText_whenMatchesAtWordAndNonBoundary_thenCorrect() {
    int matches = runTest("\\bdog\\B", "snoop dogg is a rapper");
    assertTrue(matches > 0);
}

Pattern Class Methods

之前,我們只以基本方式創(chuàng)建了模式對象。然而,這個類有另一個compile方法的變體,它接受一組標志以及影響模式匹配方式的regex參數(shù)。

這些標志只是抽象的整數(shù)值。讓我們重載test類中的runTest方法,以便它可以將標志作為第三個參數(shù):

public static int runTest(String regex, String text, int flags) {
    pattern = Pattern.compile(regex, flags);
    matcher = pattern.matcher(text);
    int matches = 0;
    while (matcher.find()){
        matches++;
    }
    return matches;
}

在本節(jié)中,我們將了解不同的支持標志以及它們的使用方式。

Pattern.CANON_EQ

此標志啟用canonical equivalence,當且僅當兩個字符的完整規(guī)范分解匹配時,才會認為這兩個字符匹配。

考慮帶重音的Unicode字符é。它的復(fù)合代碼點是u00E9。但是,Unicode的組成字符e、u0065u0301也有單獨的代碼點。在這種情況下,合成字符u00E9與雙字符序列u0065 u0301無法區(qū)分。

默認情況下,匹配不考慮規(guī)范等效:

@Test
public void givenRegexWithoutCanonEq_whenMatchFailsOnEquivalentUnicode_thenCorrect() {
    int matches = runTest("\u00E9", "\u0065\u0301");
 
    assertFalse(matches > 0);
}

但如果添加標志,則測試將通過:

@Test
public void givenRegexWithCanonEq_whenMatchesOnEquivalentUnicode_thenCorrect() {
    int matches = runTest("\u00E9", "\u0065\u0301", Pattern.CANON_EQ);
 
    assertTrue(matches > 0);
}

Pattern.CASE_INSENSITIVE

無論大小寫,此標志都支持匹配。默認情況下,匹配會考慮大小寫:

@Test
public void givenRegexWithDefaultMatcher_whenMatchFailsOnDifferentCases_thenCorrect() {
    int matches = runTest("dog", "This is a Dog");
 
    assertFalse(matches > 0);
}

因此,使用此標志,我們可以更改默認行為:

@Test
public void givenRegexWithCaseInsensitiveMatcher
  _whenMatchesOnDifferentCases_thenCorrect() {
    int matches = runTest(
      "dog", "This is a Dog", Pattern.CASE_INSENSITIVE);
 
    assertTrue(matches > 0);
}

我們還可以使用等效的嵌入標志表達式來實現(xiàn)相同的結(jié)果:

@Test
public void givenRegexWithEmbeddedCaseInsensitiveMatcher
  _whenMatchesOnDifferentCases_thenCorrect() {
    int matches = runTest("(?i)dog", "This is a Dog");
 
    assertTrue(matches > 0);
}

Pattern.COMMENTS

Java API允許在正則表達式中包含使用#的注釋。這有助于記錄復(fù)雜的正則表達式,而其他程序員可能無法立即看到這些正則表達式。

comments標志使matcher忽略正則表達式中的任何空白或注釋,只考慮模式。

在默認匹配模式下,以下測試將失?。?/strong>

@Test
public void givenRegexWithComments_whenMatchFailsWithoutFlag_thenCorrect() {
    int matches = runTest(
      "dog$  #check for word dog at end of text", "This is a dog");
 
    assertFalse(matches > 0);
}

這是因為匹配器將在輸入文本中查找整個正則表達式,包括空格和#字符。但當我們使用該標志時,它將忽略額外的空格,并且以#開頭的每個文本都將被視為每行要忽略的注釋:

@Test
public void givenRegexWithComments_whenMatchesWithFlag_thenCorrect() {
    int matches = runTest(
      "dog$  #check end of text","This is a dog", Pattern.COMMENTS);
 
    assertTrue(matches > 0);
}

還有一個替代的嵌入式標志表達式:

@Test
public void givenRegexWithComments_whenMatchesWithEmbeddedFlag_thenCorrect() {
    int matches = runTest(
      "(?x)dog$  #check end of text", "This is a dog");
 
    assertTrue(matches > 0);
}

到此,相信大家對“Java正則表達式API邊界匹配怎么實現(xiàn)”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI