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
猜你喜欢
- 动态联接库(DLL)是加快应用程序关键部分的执行速度的重要方法,但有一点恐怕大部分人都不知道,那就是在ASP文件也能通过调用DLL来加快服务
- SQL Server 2005相对于SQL Server 2000做了很大的改进,许些新特性是非常实用的。本文中将通过几个具体示例进行详细的
- 一.修改/etc/my.cnf文件default-character-set=utf8[mysqld]datadir= ar b/mysql
- javascript实现翻页效果:<html> <head> <title>上下翻页看 - aspxho
- 今天在一个QQ群中看到有人在问一个进度条的实现方式,当时因为工作时间,需求相对也比较紧,只是简单的说了一下可以通过CSS的边框属性和背景属性
- 同伪类的方式类似,伪元素通过对插人到文档中的虚构元素进行触发,从而达到某种效果。在CSS1里,有两个伪元素,即:first-letter和f
- 一、MySQL数据库的实例管理器概述:1、MySQL数据库的实例管理器(IM)是通过TCP/IP端口运行的后台程序,用来监视和管理MySQL
- 让 PHP 支持 MySQLPHP 有专有的 MySQL 函数库以使用操作 MYSQL 数据库。在 PHP 5 及以后版本中不再
- 以下为谷歌国际少年绘画大赛,小学1-3年级作品。看孩子们的logo创意,自愧呀! 彩色世界我们热爱温暖的阳光,“O”代表我们心中的太阳。我们
- “你不必严格遵守这些原则,违背它们也不会被处以宗教刑罚。但你应当把这些原则看成警铃,若违背了其中的一条,那么警铃就会响起
- 1.今天网上下载一个博客项目,发现本地访问,js,css加载不了.我想应该是项目上线的安全措施,但是我想调试项目.找到方法如下在settin
- 前端开发部门在壮大,人员在增加,技术在进步。CSSer虽然不能代表前端开发团队,但CSSer是前端开发中非常重要的组成部分。一、文档写程序的
- 写过一篇"正则表达式30分钟入门教程",有读者问:[^abc]表示不包含a、b、c中任意字符, 我想实现不包含字符串ab
- ORACLE访问SQL SERVER数据库有一篇《Oracle 异构服务实践》讲得很清楚。但里面没有讲如何设置访问多个SQL Server数
- 无法远程登入MySQL数据库的几种解决办法方法一:尝试用MySQL Adminstrator GUI Tool登入MySQL Server,
- 想要在准备查询混合数据库系统时将SQL Server转换为 XQuery 和进行反向转换吗?了解如何开发 Web 服务来从SQL Serve
- 如何验证日期输入是否正确?我们可以利用ASP 3.0里的On Error Resume Next来完成这项功能:<%Option&nb
- 天我们看看import的有关内容。编程时总是用到import导入,动不动就导入,很简单,但import到底是个什么功能,它的本质是什么?一.
- 引言委托 和 事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易。它们就
- SQL Server有两种备份方式,一种是使用BACKUP DATABASE将数据库文件备份出去,另外一种就是直接拷贝数据库文件mdf和日志