Java正则表达式API Matcher类方法

 更新时间:2022年06月10日 09:11:52   作者: sofia   
这篇文章主要介绍了Java正则表达式API Matcher类方法,对Matcher类的一些有用方法进行功能对它们进行分组展开介绍,需要的朋友可以参考一下

一、Pattern.DOTALL

默认情况下,当我们使用“.”时表达式中,我们将匹配输入字符串中的每个字符,直到遇到新行字符。

使用此标志,匹配也将包括行终止符。我们将通过以下示例更好地理解。这些例子将略有不同。由于我们感兴趣的是针对匹配的字符串进行断言,因此我们将使用matcher的group方法来返回之前的匹配。

首先,我们将看到默认行为:

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

正如我们所看到的,只有行终止符之前输入的第一部分匹配。

现在在dotall模式下,包括行终止符在内的整个文本都将匹配:

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

我们还可以使用嵌入的标志表达式来启用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对任何元字符、转义字符或正则表达式语法都没有特殊意义。如果没有此标志,匹配器将根据任何输入字符串匹配以下正则表达式:

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

这是我们在所有示例中看到的默认行为。但是,使用此标志时,将找不到匹配项,因为匹配器将查找(.*),而不是解释它:

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

现在,如果我们添加所需的字符串,测试将通过:

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

没有用于启用文字分析的嵌入标志字符。

三、Pattern.MULTILINE

默认情况下,^$元字符分别在整个输入字符串的开头和结尾绝对匹配。匹配器忽略任何行终止符:

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

匹配失败,因为匹配器在整个字符串的末尾搜索dog,但狗出现在字符串第一行的末尾。

然而,有了这个标志,同样的测试也会通过,因为匹配器现在考虑了行终止符。因此,字符串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);
}

以下是嵌入式标志版本:

@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类方法

在本节中,我们将研究Matcher类的一些有用方法。为了清晰起见,我们将根据功能对它们进行分组。

索引方法

索引方法提供有用的索引值,精确显示在输入字符串中找到匹配项的位置。在下面的测试中,我们将确认输入字符串中dog匹配的开始和结束索引:

@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方法遍历输入字符串并返回一个布尔值,指示是否找到该模式。常用的是matcheslookingAt方法。

matcheslookingAt方法都试图根据模式匹配输入序列。不同之处在于,匹配需要匹配整个输入序列,而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方法替换与给定正则表达式匹配的文本。正如其名称所示,replaceFirst替换第一个引用,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方法允许我们用相同的替换替换所有匹配项。

到此这篇关于Java正则表达式API Matcher类方法的文章就介绍到这了,更多相关Java正则表达式 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java设计模式之单例模式的详解及优点

    java设计模式之单例模式的详解及优点

    这篇文章主要介绍了java设计模式之单例模式的详解及优点的相关资料,如果一个类始终只能创建一个实例,那么这个类被称为单例类,这种设计模式被称为单例模式,需要的朋友可以参考下
    2017-08-08
  • Java中的XML解析技术详析

    Java中的XML解析技术详析

    XML文档是一个文档树,从根部开始,并扩展到树的最底部,下面这篇文章主要给大家介绍了关于Java中XML解析技术的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-08-08
  • Springboot整合SpringSecurity的完整案例详解

    Springboot整合SpringSecurity的完整案例详解

    Spring Security是基于Spring生态圈的,用于提供安全访问控制解决方案的框架,Spring Security登录认证主要涉及两个重要的接口 UserDetailService和UserDetails接口,本文对Springboot整合SpringSecurity过程给大家介绍的非常详细,需要的朋友参考下吧
    2024-01-01
  • Java设计模式之策略模式深入刨析

    Java设计模式之策略模式深入刨析

    策略模式属于Java 23种设计模式中行为模式之一,该模式定义了一系列算法,并将每个算法封装起来,使它们可以相互替换,且算法的变化不会影响使用算法的客户。本文将通过示例详细讲解这一模式,需要的可以参考一下
    2022-05-05
  • Java使用NioSocket手动实现HTTP服务器

    Java使用NioSocket手动实现HTTP服务器

    本篇文章主要介绍了Java使用NioSocket手动实现HTTP服务器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-05-05
  • Spring Boot Admin Server管理客户端过程详解

    Spring Boot Admin Server管理客户端过程详解

    这篇文章主要介绍了Spring Boot Admin Server管理客户端过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • SpringBoot去除内嵌tomcat的实现

    SpringBoot去除内嵌tomcat的实现

    这篇文章主要介绍了SpringBoot去除内嵌tomcat的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • java实现计算周期性提醒的示例

    java实现计算周期性提醒的示例

    本文分享一个java实现计算周期性提醒的示例,可以计算父亲节、母亲节这样的节日,也可以定义如每月最好一个周五,以方便安排会议
    2014-04-04
  • Java之进程和线程的区别

    Java之进程和线程的区别

    这篇文章主要介绍了进程与线程的区别,线程具有许多传统进程所具有的特征,而把传统的进程称为重型进程(Heavy—Weight Process),它相当于只有一个线程的任务,有感兴趣的小伙伴可以参考阅读本文
    2023-03-03
  • jpa介绍以及在spring boot中使用详解

    jpa介绍以及在spring boot中使用详解

    最近在项目中使用了一下jpa,发现还是挺好用的。这里就来讲一下jpa以及在spring boot中的使用。在这里我们先来了解一下jpa,希望能给你带来帮助
    2021-08-08

最新评论