Java正则表达式API边界匹配
作者:sofia 发布时间:2023-07-03 19:36:18
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);
}
来源:https://javakk.com/2658.html


猜你喜欢
- 本文实例讲述了Python网络编程使用select实现socket全双工异步通信功能。分享给大家供大家参考,具体如下:在前面一篇《Pytho
- 作为开发者,我们可以通过以下3中方式来配置logging:1)使用Python代码显式的创建loggers, handlers和format
- 本文实例为大家分享了python爬取网易云音乐评论的具体代码,供大家参考,具体内容如下import requestsimport bs4im
- 数组的定义:方法1.var mycars=new Array()mycars[0]="Saab"mycars[1]=&q
- 本文实例讲述了Python面向对象之私有属性和私有方法。分享给大家供大家参考,具体如下:01. 应用场景及定义方式应用场景在实际开发中,对象
- 1. 索引及切片数组中的元素可以通过索引以及切片的手段进行访问或者修改,和列表的切片操作一样。下面直接使用代码进行实现,具体操作方式以及意义
- 本文实例讲述了Selenium基本用法。分享给大家供大家参考,具体如下:Selenium是一个用于Web应用程序测试的工具。Selenium
- 对于很多开发者来说,Navicat这个软件并不陌生, 相信这个彩虹色图标的软件,有效的帮助了你的开发工作。从前上学的时候,我都是用的都是从网
- 1. python三维图表绘制方法简介python三维图表的绘制算是二维图表的一个进阶版本,本质上和二维图表的绘制并无差别,唯一的区别在于使
- 我是使用源码编译的方式安装的,网上有的可以添加 ppa 源进行在线安装,但我试了行不通,所以还是采用源码安装1、安装编译依赖项sudo ap
- JavaScript 函数调用JavaScript 函数有 4 种调用方式。每种方式的不同方式在于 this 的初始化。this 关键字一般
- 本文实例讲述了Python机器学习算法库scikit-learn学习之决策树实现方法。分享给大家供大家参考,具体如下:决策树决策树(DTs)
- 这篇文章主要介绍了Python读取表格类型文件代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋
- 算法比较暴力,直接用穷举的方式一个一个去试,所以程序运行时间会比较长,运行时间视数独而定。不过从一开始到运行成功,整个过程却是一波三折,设计
- 朋友的网站要计算机票的折扣价格,并且在最后的折扣价格上应对个位进行四舍五入,同时在ASP和Javasc
- 概述Vue 在插入、更新或者移除 DOM 时,提供多种不同方式的应用过渡效果。包括以下工具:在 CSS 过渡和动画中自动应用 class可以
- 如下所示:#coding=utf8import csv import logginglogging.basicConfig(level=lo
- 一、采用?a=1&b=2访问修改views.py:views.pyfrom django.shortcuts import rend
- pytorch 预训练模型读取修改相关参数的填坑修改部分层,仍然调用之前的模型参数。resnet = resnet50(pretrained
- 本文实例讲述了Python推导式。分享给大家供大家参考,具体如下:1. 列表推导式>>> li = [1,2,3,4,5,