欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java正則表達(dá)式API Matcher類方法

 更新時(shí)間:2022年06月10日 09:11:52   作者:?sofia?  
這篇文章主要介紹了Java正則表達(dá)式API Matcher類方法,對Matcher類的一些有用方法進(jìn)行功能對它們進(jìn)行分組展開介紹,需要的朋友可以參考一下

一、Pattern.DOTALL

默認(rèn)情況下,當(dāng)我們使用“.”時(shí)表達(dá)式中,我們將匹配輸入字符串中的每個(gè)字符,直到遇到新行字符。

使用此標(biāo)志,匹配也將包括行終止符。我們將通過以下示例更好地理解。這些例子將略有不同。由于我們感興趣的是針對匹配的字符串進(jìn)行斷言,因此我們將使用matcher的group方法來返回之前的匹配。

首先,我們將看到默認(rèn)行為:

@Test
public void givenRegexWithLineTerminator_whenMatchFails_thenCorrect() {
    Pattern pattern = Pattern.compile("(.*)");
    Matcher matcher = pattern.matcher(
      "this is a text" + System.getProperty("line.separator") 
        + " continued on another line");
    matcher.find();
 
    assertEquals("this is a text", matcher.group(1));
}

正如我們所看到的,只有行終止符之前輸入的第一部分匹配。

現(xiàn)在在dotall模式下,包括行終止符在內(nèi)的整個(gè)文本都將匹配:

@Test
public void givenRegexWithLineTerminator_whenMatchesWithDotall_thenCorrect() {
    Pattern pattern = Pattern.compile("(.*)", Pattern.DOTALL);
    Matcher matcher = pattern.matcher(
      "this is a text" + System.getProperty("line.separator") 
        + " continued on another line");
    matcher.find();
    assertEquals(
      "this is a text" + System.getProperty("line.separator") 
        + " continued on another line", matcher.group(1));
}

我們還可以使用嵌入的標(biāo)志表達(dá)式來啟用dotall模式:

@Test
public void givenRegexWithLineTerminator_whenMatchesWithEmbeddedDotall
  _thenCorrect() {
    
    Pattern pattern = Pattern.compile("(?s)(.*)");
    Matcher matcher = pattern.matcher(
      "this is a text" + System.getProperty("line.separator") 
        + " continued on another line");
    matcher.find();
 
    assertEquals(
      "this is a text" + System.getProperty("line.separator") 
        + " continued on another line", matcher.group(1));
}

二、Pattern.LITERAL

在此模式下,matcher對任何元字符、轉(zhuǎn)義字符或正則表達(dá)式語法都沒有特殊意義。如果沒有此標(biāo)志,匹配器將根據(jù)任何輸入字符串匹配以下正則表達(dá)式:

@Test
public void givenRegex_whenMatchesWithoutLiteralFlag_thenCorrect() {
    int matches = runTest("(.*)", "text");
 
    assertTrue(matches > 0);
}

這是我們在所有示例中看到的默認(rèn)行為。但是,使用此標(biāo)志時(shí),將找不到匹配項(xiàng),因?yàn)槠ヅ淦鲗⒉檎?code>(.*),而不是解釋它:

@Test
public void givenRegex_whenMatchFailsWithLiteralFlag_thenCorrect() {
    int matches = runTest("(.*)", "text", Pattern.LITERAL);
 
    assertFalse(matches > 0);
}

現(xiàn)在,如果我們添加所需的字符串,測試將通過:

@Test
public void givenRegex_whenMatchesWithLiteralFlag_thenCorrect() {
    int matches = runTest("(.*)", "text(.*)", Pattern.LITERAL);
 
    assertTrue(matches > 0);
}

沒有用于啟用文字分析的嵌入標(biāo)志字符。

三、Pattern.MULTILINE

默認(rèn)情況下,^$元字符分別在整個(gè)輸入字符串的開頭和結(jié)尾絕對匹配。匹配器忽略任何行終止符:

@Test
public void givenRegex_whenMatchFailsWithoutMultilineFlag_thenCorrect() {
    int matches = runTest(
      "dog$", "This is a dog" + System.getProperty("line.separator") 
      + "this is a fox");
 
    assertFalse(matches > 0);
}

匹配失敗,因?yàn)槠ヅ淦髟谡麄€(gè)字符串的末尾搜索dog,但狗出現(xiàn)在字符串第一行的末尾。

然而,有了這個(gè)標(biāo)志,同樣的測試也會(huì)通過,因?yàn)槠ヅ淦鳜F(xiàn)在考慮了行終止符。因此,字符串dog正好在行終止之前找到,因此成功:

@Test
public void givenRegex_whenMatchesWithMultilineFlag_thenCorrect() {
    int matches = runTest(
      "dog$", "This is a dog" + System.getProperty("line.separator") 
      + "this is a fox", Pattern.MULTILINE);
 
    assertTrue(matches > 0);
}

以下是嵌入式標(biāo)志版本:

@Test
public void givenRegex_whenMatchesWithEmbeddedMultilineFlag_
  thenCorrect() {
    int matches = runTest(
      "(?m)dog$", "This is a dog" + System.getProperty("line.separator") 
      + "this is a fox");
 
    assertTrue(matches > 0);
}

四、Matcher類方法

在本節(jié)中,我們將研究Matcher類的一些有用方法。為了清晰起見,我們將根據(jù)功能對它們進(jìn)行分組。

索引方法

索引方法提供有用的索引值,精確顯示在輸入字符串中找到匹配項(xiàng)的位置。在下面的測試中,我們將確認(rèn)輸入字符串中dog匹配的開始和結(jié)束索引:

@Test
public void givenMatch_whenGetsIndices_thenCorrect() {
    Pattern pattern = Pattern.compile("dog");
    Matcher matcher = pattern.matcher("This dog is mine");
    matcher.find();
 
    assertEquals(5, matcher.start());
    assertEquals(8, matcher.end());
}

Study方法

Study方法遍歷輸入字符串并返回一個(gè)布爾值,指示是否找到該模式。常用的是matcheslookingAt方法。

matcheslookingAt方法都試圖根據(jù)模式匹配輸入序列。不同之處在于,匹配需要匹配整個(gè)輸入序列,而lookingAt則不需要。

這兩種方法都從輸入字符串的開頭開始:

@Test
public void whenStudyMethodsWork_thenCorrect() {
    Pattern pattern = Pattern.compile("dog");
    Matcher matcher = pattern.matcher("dogs are friendly");
 
    assertTrue(matcher.lookingAt());
    assertFalse(matcher.matches());
}

matches方法將在如下情況下返回true

@Test
public void whenMatchesStudyMethodWorks_thenCorrect() {
    Pattern pattern = Pattern.compile("dog");
    Matcher matcher = pattern.matcher("dog");
 
    assertTrue(matcher.matches());
}

Replacement方法

Replacement方法可用于替換輸入字符串中的文本。常見的是replaceFirstreplaceAll。

replaceFirstreplaceAll方法替換與給定正則表達(dá)式匹配的文本。正如其名稱所示,replaceFirst替換第一個(gè)引用,replaceAll替換所有引用:

@Test
public void whenReplaceFirstWorks_thenCorrect() {
    Pattern pattern = Pattern.compile("dog");
    Matcher matcher = pattern.matcher(
      "dogs are domestic animals, dogs are friendly");
    String newStr = matcher.replaceFirst("cat");
 
    assertEquals(
      "cats are domestic animals, dogs are friendly", newStr);
}

替換所有引用:

@Test
public void whenReplaceAllWorks_thenCorrect() {
    Pattern pattern = Pattern.compile("dog");
    Matcher matcher = pattern.matcher(
      "dogs are domestic animals, dogs are friendly");
    String newStr = matcher.replaceAll("cat");
 
    assertEquals("cats are domestic animals, cats are friendly", newStr);
}

replaceAll方法允許我們用相同的替換替換所有匹配項(xiàng)。

到此這篇關(guān)于Java正則表達(dá)式API Matcher類方法的文章就介紹到這了,更多相關(guān)Java正則表達(dá)式 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論