您的位置 首页 java

Java正则表达式API之系列(三)

Boundary Matchers

Java regex API还支持边界匹配。如果我们关心在输入文本中匹配的确切位置,那么这就是我们要寻找的。在前面的示例中,我们关心的只是是否找到匹配项。

为了仅在文本开头所需的正则表达式为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);
}  

如果仅在单词边界处找到所需文本时才需要匹配,则在 正则表达式 的开头和结尾使用 \\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);
}  

这些测试之所以通过,是因为 字符串 的开头以及文本之间的空格标记了单词边界,但是以下测试显示了相反的结果:

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

一行中出现的两个单词字符不会标记单词边界,但我们可以通过更改正则表达式的结尾来查找非单词边界:

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

Pattern Class Methods

之前,我们只以基本方式创建了模式对象。然而,这个类有另一个compile方法的变体,它接受一组标志以及影响模式匹配方式的 regex 参数。

这些标志只是抽象的整数值。让我们重载test类中的runTest方法,以便它可以将标志作为第三个参数:

 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;
}  

在本节中,我们将了解不同的支持标志以及它们的使用方式。

Pattern.CANON_EQ

此标志启用 canonical equivalence ,当且仅当两个字符的完整规范分解匹配时,才会认为这两个字符匹配。

考虑带重音的 Unicode 字符 é 。它的复合代码点是什么 u00E9 。但是,Unicode的组成字符 e u0065 u0301 也有单独的代码点。在这种情况下,合成字符 u00E9 与双字符序列 u0065 u0301 无法区分。

默认情况下,匹配不考虑规范等效:

 @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);
}  

我们还可以使用等效的嵌入标志表达式来实现相同的结果:

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

Pattern.COMMENTS

Java API 允许在正则表达式中包含使用#的注释。这有助于记录复杂的正则表达式,而其他程序员可能无法立即看到这些正则表达式。

comments标志使matcher忽略正则表达式中的任何空白或注释,只考虑模式。在默认匹配模式下,以下测试将失败:

 @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之系列(三)

文章地址:https://www.zhihuclub.com/193225.shtml

关于作者: 智云科技

热门文章

网站地图