首页 热点 业界 科技快讯 数码 电子消费 通信 前沿动态 电商

Java正则表达式API边界匹配

2022-06-10 10:01:58 来源 : 软件开发网

目录

Boundary Matchers

Pattern Class Methods

Pattern.CANON_EQ

Pattern.CASE_INSENSITIVE

Pattern.COMMENTS

Boundary Matchers

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

为了仅在文本开头所需的正则表达式为true时匹配,我们使用插入符号^。

此测试将失败,因为可以在开头找到文本dog:

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

下面的测试将失败:

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

为了仅在文本末尾所需的正则表达式为true时匹配,我们使用美元字符$。在以下情况下会找到匹配项:

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

并且没有找到匹配:

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

如果仅在单词边界处找到所需文本时才需要匹配,则在正则表达式的开头和结尾使用\\b正则表达式:

空格是单词边界:

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

行首的空字符串也是单词边界:

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

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

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

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

@Testpublic 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的组成字符eu0065u0301也有单独的代码点。在这种情况下,合成字符u00E9与双字符序列u0065 u0301无法区分。

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

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

但如果添加标志,则测试将通过:

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

无论大小写,此标志都支持匹配。默认情况下,匹配会考虑大小写:

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

因此,使用此标志,我们可以更改默认行为:

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

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

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

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

comments标志使matcher忽略正则表达式中的任何空白或注释,只考虑模式。

在默认匹配模式下,以下测试将失败:

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

这是因为匹配器将在输入文本中查找整个正则表达式,包括空格和#字符。但当我们使用该标志时,它将忽略额外的空格,并且以#开头的每个文本都将被视为每行要忽略的注释:

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

还有一个替代的嵌入式标志表达式:

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

到此这篇关于Java正则表达式API边界匹配的文章就介绍到这了,更多相关Java正则表达式内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!

标签: 正则表达式 单词边界 支持软件

相关文章

最近更新
观焦点:超萌相机 2023-03-01 12:29:37
海南百货网 2023-03-01 12:13:44
焦点热讯:宜点充 2023-02-28 10:10:16
天天关注:小铺CEO 2023-02-28 10:07:13
【世界聚看点】KaFit 2023-02-28 09:31:37
葱天下 2023-02-28 09:17:03
渔界竞钓 2023-02-28 08:15:29
焦点快看:鲸奇视频 2023-02-28 06:30:37
环球热议:萝小逗 2023-02-27 23:25:49
简讯:小码公交 2023-02-27 23:16:12
彼岸花 2023-02-27 22:32:52
时时夺宝 2023-02-27 21:37:50
天天动态:袜之源 2023-02-27 21:29:50
天天资讯:AI空气 2023-02-27 20:19:46
世界时讯:绘读 2023-02-27 20:19:41
看点:一元得购 2023-02-27 19:26:28