Module java.base
Package java.util.regex

Class Matcher

java.lang.Object
java.util.regex.Matcher
所有已实现的接口:
MatchResult

public final class Matcher extends Object implements MatchResult
执行对字符序列进行匹配操作的引擎,通过解释Pattern来执行。

通过调用模式的matcher方法从模式创建匹配器。创建后,匹配器可用于执行三种不同类型的匹配操作:

  • matches方法尝试将整个输入序列与模式进行匹配。

  • lookingAt方法尝试从开头开始将输入序列与模式进行匹配。

  • find方法扫描输入序列,查找与模式匹配的下一个子序列。

这些方法中的每一个都返回一个布尔值,指示成功或失败。可以通过查询匹配器的状态来获取有关成功匹配的更多信息。

匹配器在其输入的子集中查找匹配项,称为区域。默认情况下,区域包含匹配器的所有输入。可以通过region方法修改区域,并通过regionStartregionEnd方法查询区域。区域边界与某些模式构造的交互方式可以更改。有关更多详细信息,请参见useAnchoringBoundsuseTransparentBounds

该类还定义了用于用新字符串替换匹配的子序列的方法,如果需要,可以从匹配结果计算其内容。可以同时使用appendReplacementappendTail方法将结果收集到现有的字符串缓冲区或字符串生成器中。或者,可以使用更方便的replaceAll方法创建一个字符串,其中输入序列中的每个匹配子序列都被替换。

匹配器的显式状态包括最近成功匹配的开始和结束索引。它还包括由模式中的每个capturing group捕获的输入子序列的开始和结束索引,以及这样的子序列的总计数。为方便起见,还提供了以字符串形式返回这些捕获的子序列的方法。

匹配器的显式状态最初是未定义的;在成功匹配之前尝试查询任何部分将导致抛出IllegalStateException。匹配操作会重新计算匹配器的显式状态。

匹配器的隐式状态包括输入字符序列以及追加位置,它最初为零,并通过appendReplacement方法进行更新。

可以通过显式调用reset()方法或者如果需要新的输入序列,则调用reset(CharSequence)方法来显式重置匹配器。重置匹配器会丢弃其显式状态信息,并将追加位置设置为零。

此类的实例不适用于多个并发线程。

自:
1.4
  • Method Details

    • pattern

      public Pattern pattern()
      返回此匹配器解释的模式。
      返回:
      创建此匹配器的模式
    • toMatchResult

      public MatchResult toMatchResult()
      将此匹配器的匹配状态作为MatchResult返回。结果不受对此匹配器执行的后续操作的影响。
      返回:
      具有此匹配器状态的MatchResult
      自:
      1.5
    • usePattern

      public Matcher usePattern(Pattern newPattern)
      更改此Matcher用于查找匹配项的Pattern

      此方法会导致此匹配器丢失有关上次匹配组的信息。匹配器在输入中的位置保持不变,其最后追加位置不受影响。

      参数:
      newPattern - 此匹配器使用的新模式
      返回:
      此匹配器
      抛出:
      IllegalArgumentException - 如果newPattern为null
      自:
      1.5
    • reset

      public Matcher reset()
      重置此匹配器。

      重置匹配器会丢弃其所有显式状态信息,并将其追加位置设置为零。匹配器的区域设置为默认区域,即其整个字符序列。此匹配器的区域边界的锚定和透明度不受影响。

      返回:
      此匹配器
    • reset

      public Matcher reset(CharSequence input)
      重置此匹配器并使用新的输入序列。

      重置匹配器会丢弃其所有显式状态信息,并将其追加位置设置为零。匹配器的区域设置为默认区域,即其整个字符序列。此匹配器的区域边界的锚定和透明度不受影响。

      参数:
      input - 新的输入字符序列
      返回:
      此匹配器
    • start

      public int start()
      返回上一次匹配的开始索引。
      指定由:
      start 在接口 MatchResult
      返回:
      第一个匹配字符的索引
      抛出:
      IllegalStateException - 如果尚未尝试匹配,或者上一个匹配操作失败
    • start

      public int start(int group)
      返回给定组在上一个匹配操作期间捕获的子序列的起始索引。

      捕获组 从左到右索引,从1开始。组零表示整个模式,因此表达式 m.start(0) 等同于 m.start()

      指定由:
      start 在接口 MatchResult
      参数:
      group - 该匹配器模式中捕获组的索引
      返回:
      由该组捕获的第一个字符的索引,如果匹配成功但组本身未匹配任何内容,则返回-1
      抛出:
      IllegalStateException - 如果尚未尝试匹配,或者上一个匹配操作失败
      IndexOutOfBoundsException - 如果模式中没有具有给定索引的捕获组
    • start

      public int start(String name)
      返回给定命名捕获组 在上一个匹配操作期间捕获的子序列的起始索引。
      指定由:
      start 在接口 MatchResult
      参数:
      name - 该匹配器模式中命名捕获组的名称
      返回:
      由该组捕获的第一个字符的索引,如果匹配成功但组本身未匹配任何内容,则返回-1
      抛出:
      IllegalStateException - 如果尚未尝试匹配,或者上一个匹配操作失败
      IllegalArgumentException - 如果模式中没有具有给定名称的捕获组
      自1.8起
    • end

      public int end()
      返回匹配的最后一个字符之后的偏移量。
      指定由:
      end 在接口 MatchResult
      返回:
      匹配的最后一个字符之后的偏移量
      抛出:
      IllegalStateException - 如果尚未尝试匹配,或者上一个匹配操作失败
    • end

      public int end(int group)
      返回给定组在上一个匹配操作期间捕获的子序列的最后一个字符之后的偏移量。

      捕获组 从左到右索引,从1开始。组零表示整个模式,因此表达式 m.end(0) 等同于 m.end()

      指定由:
      end 在接口 MatchResult
      参数:
      group - 该匹配器模式中捕获组的索引
      返回:
      由该组捕获的最后一个字符之后的偏移量,如果匹配成功但组本身未匹配任何内容,则返回-1
      抛出:
      IllegalStateException - 如果尚未尝试匹配,或者上一个匹配操作失败
      IndexOutOfBoundsException - 如果模式中没有具有给定索引的捕获组
    • end

      public int end(String name)
      返回给定命名捕获组 在上一个匹配操作期间捕获的子序列的最后一个字符之后的偏移量。
      指定由:
      end 在接口 MatchResult
      参数:
      name - 该匹配器模式中命名捕获组的名称
      返回:
      由该组捕获的最后一个字符之后的偏移量,如果匹配成功但组本身未匹配任何内容,则返回-1
      抛出:
      IllegalStateException - 如果尚未尝试匹配,或者上一个匹配操作失败
      IllegalArgumentException - 如果模式中没有具有给定名称的捕获组
      自1.8起
    • group

      public String group()
      返回上一个匹配的输入子序列。

      对于匹配器 m,输入序列 s,表达式 m.group()s.substring(m.start(), m. end()) 是等效的。

      请注意,某些模式,例如 a*,匹配空字符串。当模式成功匹配输入中的空字符串时,此方法将返回空字符串。

      指定由:
      group 在接口 MatchResult
      返回:
      上一个匹配的(可能为空的)子序列,以字符串形式返回,如果具有先前匹配的匹配器已更改其Pattern,但尚未尝试新匹配,则返回null
      抛出:
      IllegalStateException - 如果尚未尝试匹配,或者上一个匹配操作失败
    • group

      public String group(int group)
      返回上一个匹配操作期间由给定组捕获的输入子序列。

      对于匹配器 m,输入序列 s,和组索引 g,表达式 m.group(g)s.substring(m.start(g ), m.end(g)) 是等效的。

      捕获组 从左到右索引,从1开始。组零表示整个模式,因此表达式 m.group(0) 等同于 m.group()

      如果匹配成功但指定的组未能匹配输入序列的任何部分,则返回null。请注意,某些组,例如 (a*),匹配空字符串。当这样的组成功匹配输入中的空字符串时,此方法将返回空字符串。

      指定由:
      group 在接口 MatchResult
      参数:
      group - 该匹配器模式中捕获组的索引
      返回:
      由给定组在上一个匹配操作期间捕获的(可能为空的)子序列,如果组未能匹配输入的部分或者匹配器的Pattern 在成功匹配后发生更改,但尚未尝试新匹配,则返回null
      抛出:
      IllegalStateException - 如果尚未尝试匹配,或者上一个匹配操作失败
      IndexOutOfBoundsException - 如果模式中没有具有给定索引的捕获组
    • group

      public String group(String name)
      返回给定命名捕获组 在上一个匹配操作期间捕获的输入子序列。

      如果匹配成功但指定的组未能匹配输入序列的任何部分,则返回null。请注意,某些组,例如 (a*),匹配空字符串。当这样的组成功匹配输入中的空字符串时,此方法将返回空字符串。

      指定由:
      group 在接口 MatchResult
      参数:
      name - 该匹配器模式中命名捕获组的名称
      返回:
      由给定命名组在上一个匹配操作期间捕获的(可能为空的)子序列,如果组未能匹配输入的部分,则返回null
      抛出:
      IllegalStateException - 如果尚未尝试匹配,或者上一个匹配操作失败
      IllegalArgumentException - 如果模式中没有具有给定名称的捕获组
      自1.7起
    • groupCount

      public int groupCount()
      返回该匹配器模式中捕获组的数量。

      按照惯例,组零表示整个模式。此计数不包括在内。

      小于或等于此方法返回值的任何非负整数都保证是该匹配器的有效组索引。

      指定者:
      groupCount 在接口 MatchResult
      返回值:
      此匹配器模式中捕获组的数量
    • matches

      public boolean matches()
      尝试将整个区域与模式进行匹配。

      如果匹配成功,则可以通过startendgroup方法获取更多信息。

      返回值:
      仅当整个区域序列与此匹配器模式匹配时返回true
    • find

      public boolean find()
      尝试查找输入序列的下一个与模式匹配的子序列。

      此方法从此匹配器区域的开头开始,或者如果先前调用该方法成功且自那时起匹配器尚未重置,则从上一个匹配未匹配的第一个字符开始。

      如果匹配成功,则可以通过startendgroup方法获取更多信息。

      返回值:
      仅当输入序列的子序列与此匹配器模式匹配时返回true
    • find

      public boolean find(int start)
      重置此匹配器,然后尝试从指定索引开始查找输入序列的下一个与模式匹配的子序列。

      如果匹配成功,则可以通过startendgroup方法获取更多信息,并且后续调用find()方法将从此匹配未匹配的第一个字符开始。

      参数:
      start - 开始搜索匹配的索引
      返回值:
      仅当从给定索引开始的输入序列的子序列与此匹配器模式匹配时返回true
      抛出:
      IndexOutOfBoundsException - 如果start小于零或start大于输入序列的长度
    • lookingAt

      public boolean lookingAt()
      尝试从区域的开头开始匹配输入序列与模式。

      matches方法不同,此方法始终从区域的开头开始;不需要整个区域匹配。

      如果匹配成功,则可以通过startendgroup方法获取更多信息。

      返回值:
      仅当输入序列的前缀与此匹配器模式匹配时返回true
    • quoteReplacement

      public static String quoteReplacement(String s)
      返回指定String的字面替换。此方法生成一个String,可作为Matcher类的appendReplacement方法中的字面替换s。生成的String将匹配将s视为字面序列的字符序列。反斜杠('\')和美元符号('$')不会被特殊处理。
      参数:
      s - 要转义的字符串
      返回值:
      字面字符串替换
      自:
      1.5
    • appendReplacement

      public Matcher appendReplacement(StringBuffer sb, String replacement)
      实现非终端的追加和替换步骤。

      此方法执行以下操作:

      1. 从输入序列读取字符,从追加位置开始,并将它们附加到给定的字符串缓冲区。在读取前一个匹配之前的最后一个字符之后停止,即索引start() - 1的字符。

      2. 将给定的替换字符串附加到字符串缓冲区。

      3. 将此匹配器的追加位置设置为最后一个匹配字符的索引加一,即end()

      替换字符串可能包含对先前匹配期间捕获的子序列的引用:每个${name}$g的出现将被替换为评估相应group(name)group(g)的结果。对于$g,在$之后的第一个数字始终被视为组引用的一部分。如果后续数字会形成合法的组引用,则将这些数字合并到g中。只有数字'0'到'9'被视为组引用的潜在组件。例如,如果第二个组匹配字符串"foo",那么传递替换字符串"$2bar"将导致"foobar"附加到字符串缓冲区。美元符号($)可以通过在其前面加上反斜杠(\$)作为替换字符串的文字包含在替换字符串中。

      请注意,替换字符串中的反斜杠(\)和美元符号($)可能导致结果与将其视为字面替换字符串时不同。美元符号可能被视为对上述捕获子序列的引用,并且反斜杠用于转义替换字符串中的文字字符。

      此方法旨在与appendTailfind方法一起在循环中使用。例如,以下代码将one dog two dogs in the yard写入标准输出流:

       Pattern p = Pattern.compile("cat");
       Matcher m = p.matcher("one cat two cats in the yard");
       StringBuffer sb = new StringBuffer();
       while (m.find()) {
           m.appendReplacement(sb, "dog");
       }
       m.appendTail(sb);
       System.out.println(sb.toString());
      参数:
      sb - 目标字符串缓冲区
      replacement - 替换字符串
      返回值:
      此匹配器
      抛出:
      IllegalStateException - 如果尚未尝试过匹配,或者先前的匹配操作失败
      IllegalArgumentException - 如果替换字符串引用了模式中不存在的命名捕获组
      IndexOutOfBoundsException - 如果替换字符串引用了模式中不存在的捕获组
    • appendReplacement

      public Matcher appendReplacement(StringBuilder sb, String replacement)
      实现非终端的追加和替换步骤。

      此方法执行以下操作:

      1. 从输入序列读取字符,从追加位置开始,并将它们附加到给定的字符串生成器。在读取前一个匹配之前的最后一个字符之后停止,即索引start() - 1的字符。

      2. 将给定的替换字符串附加到字符串生成器。

      3. 将此匹配器的追加位置设置为最后一个匹配字符的索引加一,即end()

      替换字符串可能包含对先前匹配期间捕获的子序列的引用:每个$g的出现将被替换为评估group(g)的结果。在$之后的第一个数字始终被视为组引用的一部分。如果后续数字会形成合法的组引用,则将这些数字合并到g中。只有数字'0'到'9'被视为组引用的潜在组件。例如,如果第二个组匹配字符串"foo",那么传递替换字符串"$2bar"将导致"foobar"附加到字符串生成器。美元符号($)可以通过在其前面加上反斜杠(\$)作为替换字符串的文字包含在替换字符串中。

      请注意,替换字符串中的反斜杠(\)和美元符号($)可能导致结果与将其视为字面替换字符串时不同。美元符号可能被视为对上述捕获子序列的引用,并且反斜杠用于转义替换字符串中的文字字符。

      此方法旨在与appendTailfind方法一起在循环中使用。例如,以下代码将one dog two dogs in the yard写入标准输出流:

       Pattern p = Pattern.compile("cat");
       Matcher m = p.matcher("one cat two cats in the yard");
       StringBuilder sb = new StringBuilder();
       while (m.find()) {
           m.appendReplacement(sb, "dog");
       }
       m.appendTail(sb);
       System.out.println(sb.toString());
      参数:
      sb - 目标字符串生成器
      replacement - 替换字符串
      返回值:
      此匹配器
      抛出:
      IllegalStateException - 如果尚未尝试过匹配,或者先前的匹配操作失败
      IllegalArgumentException - 如果替换字符串引用了模式中不存在的命名捕获组
      IndexOutOfBoundsException - 如果替换字符串引用了模式中不存在的捕获组
      自:
      9
    • appendTail

      public StringBuffer appendTail(StringBuffer sb)
      实现终端的追加和替换步骤。

      此方法从追加位置开始读取输入序列的字符,并将它们附加到给定的字符串缓冲区。在调用一个或多个appendReplacement方法后调用,以复制输入序列的其余部分。

      参数:
      sb - 目标字符串缓冲区
      返回值:
      目标字符串缓冲区
    • appendTail

      public StringBuilder appendTail(StringBuilder sb)
      实现了一个终端追加和替换步骤。

      此方法从输入序列的追加位置开始读取字符,并将它们追加到给定的字符串构建器中。它旨在在调用一个或多个appendReplacement方法之后调用,以便复制输入序列的其余部分。

      参数:
      sb - 目标字符串构建器
      返回值:
      目标字符串构建器
      自版本:
      9
    • replaceAll

      public String replaceAll(String replacement)
      用给定的替换字符串替换与模式匹配的输入序列的每个子序列。

      此方法首先重置此匹配器。然后扫描输入序列,寻找模式的匹配项。不属于任何匹配的字符直接附加到结果字符串;每个匹配在结果中被替换为替换字符串。替换字符串可能包含对捕获子序列的引用,就像appendReplacement方法中一样。

      请注意,替换字符串中的反斜杠(\)和美元符号($)可能导致结果与将其视为文字替换字符串时不同。美元符号可能被视为对捕获子序列的引用,如上所述,反斜杠用于转义替换字符串中的文字字符。

      给定正则表达式a*b,输入"aabfooaabfooabfoob",替换字符串"-",对该表达式的匹配器调用此方法将产生字符串"-foo-foo-foo-"

      调用此方法会更改此匹配器的状态。如果匹配器要用于进一步的匹配操作,则应首先重置。

      参数:
      replacement - 替换字符串
      返回值:
      通过用替换字符串替换每个匹配子序列构造的字符串,根据需要替换捕获的子序列
    • replaceAll

      public String replaceAll(Function<MatchResult,String> replacer)
      用此匹配器对应于该子序列的匹配结果应用给定替换函数的结果替换输入序列的每个与模式匹配的子序列。函数抛出的异常将被传递给调用者。

      此方法首先重置此匹配器。然后扫描输入序列,寻找模式的匹配项。不属于任何匹配的字符直接附加到结果字符串;每个匹配在结果中被替换为应用返回替换字符串的替换函数。每个替换字符串可能包含对捕获子序列的引用,就像appendReplacement方法中一样。

      请注意,替换字符串中的反斜杠(\)和美元符号($)可能导致结果与将其视为文字替换字符串时不同。美元符号可能被视为对捕获子序列的引用,如上所述,反斜杠用于转义替换字符串中的文字字符。

      给定正则表达式dog,输入"zzzdogzzzdogzzz",函数mr -> mr.group().toUpperCase(),对该表达式的匹配器调用此方法将产生字符串"zzzDOGzzzDOGzzz"

      调用此方法会更改此匹配器的状态。如果匹配器要用于进一步的匹配操作,则应首先重置。

      在替换期间,替换函数不应修改此匹配器的状态。如果检测到这种修改,此方法将尽最大努力抛出ConcurrentModificationException

      传递给替换函数的每个匹配结果的状态仅在替换函数调用期间保持恒定,并且仅当替换函数不修改此匹配器的状态时。

      实现注意:
      此实现将替换函数应用于此匹配器,该匹配器是MatchResult的实例。
      参数:
      replacer - 应用于此匹配器的匹配结果并返回替换字符串的函数。
      返回值:
      通过将每个匹配子序列替换为应用替换函数到该匹配子序列的结果构造的字符串,根据需要替换捕获的子序列
      抛出:
      NullPointerException - 如果替换函数为null
      ConcurrentModificationException - 如果检测到替换函数修改了此匹配器的状态,将尽最大努力抛出
      自版本:
      9
    • results

      public Stream<MatchResult> results()
      Returns a stream of match results for each subsequence of the input sequence that matches the pattern. The match results occur in the same order as the matching subsequences in the input sequence.

      Each match result is produced as if by toMatchResult().

      This method does not reset this matcher. Matching starts on initiation of the terminal stream operation either at the beginning of this matcher's region, or, if the matcher has not since been reset, at the first character not matched by a previous match.

      If the matcher is to be used for further matching operations after the terminal stream operation completes then it should be first reset.

      This matcher's state should not be modified during execution of the returned stream's pipeline. The returned stream's source Spliterator is fail-fast and will, on a best-effort basis, throw a ConcurrentModificationException if such modification is detected.

      Returns:
      a sequential stream of match results.
      Since:
      9
    • replaceFirst

      public String replaceFirst(String replacement)
      Replaces the first subsequence of the input sequence that matches the pattern with the given replacement string.

      This method first resets this matcher. It then scans the input sequence looking for a match of the pattern. Characters that are not part of the match are appended directly to the result string; the match is replaced in the result by the replacement string. The replacement string may contain references to captured subsequences as in the appendReplacement method.

      Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.

      Given the regular expression dog, the input "zzzdogzzzdogzzz", and the replacement string "cat", an invocation of this method on a matcher for that expression would yield the string "zzzcatzzzdogzzz".

      Invoking this method changes this matcher's state. If the matcher is to be used in further matching operations then it should first be reset.

      Parameters:
      replacement - The replacement string
      Returns:
      The string constructed by replacing the first matching subsequence by the replacement string, substituting captured subsequences as needed
    • replaceFirst

      public String replaceFirst(Function<MatchResult,String> replacer)
      Replaces the first subsequence of the input sequence that matches the pattern with the result of applying the given replacer function to the match result of this matcher corresponding to that subsequence. Exceptions thrown by the replace function are relayed to the caller.

      This method first resets this matcher. It then scans the input sequence looking for a match of the pattern. Characters that are not part of the match are appended directly to the result string; the match is replaced in the result by the applying the replacer function that returns a replacement string. The replacement string may contain references to captured subsequences as in the appendReplacement method.

      Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.

      Given the regular expression dog, the input "zzzdogzzzdogzzz", and the function mr -> mr.group().toUpperCase(), an invocation of this method on a matcher for that expression would yield the string "zzzDOGzzzdogzzz".

      Invoking this method changes this matcher's state. If the matcher is to be used in further matching operations then it should first be reset.

      The replacer function should not modify this matcher's state during replacement. This method will, on a best-effort basis, throw a ConcurrentModificationException if such modification is detected.

      The state of the match result passed to the replacer function is guaranteed to be constant only for the duration of the replacer function call and only if the replacer function does not modify this matcher's state.

      Implementation Note:
      This implementation applies the replacer function to this matcher, which is an instance of MatchResult.
      Parameters:
      replacer - The function to be applied to the match result of this matcher that returns a replacement string.
      Returns:
      The string constructed by replacing the first matching subsequence with the result of applying the replacer function to the matched subsequence, substituting captured subsequences as needed.
      Throws:
      NullPointerException - if the replacer function is null
      ConcurrentModificationException - if it is detected, on a best-effort basis, that the replacer function modified this matcher's state
      Since:
      9
    • region

      public Matcher region(int start, int end)
      设置此匹配器的区域限制。区域是将被搜索以找到匹配项的输入序列的部分。调用此方法会重置匹配器,然后将区域设置为从由start参数指定的索引开始,到由end参数指定的索引结束。

      根据使用的透明度和锚定(请参阅useTransparentBoundsuseAnchoringBounds),某些构造(如锚点)在区域边界处可能会有不同的行为。

      参数:
      start - 开始搜索的索引(包括)
      end - 结束搜索的索引(不包括)
      返回:
      此匹配器
      抛出:
      IndexOutOfBoundsException - 如果start或end小于零,如果start大于输入序列的长度,如果end大于输入序列的长度,或者如果start大于end。
      自版本:
      1.5
    • regionStart

      public int regionStart()
      报告此匹配器区域的起始索引。此匹配器进行的搜索仅限于在regionStart(包括)和regionEnd(不包括)内找到匹配项。
      返回:
      此匹配器区域的起始点
      自版本:
      1.5
    • regionEnd

      public int regionEnd()
      报告此匹配器区域的结束索引(不包括)。此匹配器进行的搜索仅限于在regionStart(包括)和regionEnd(不包括)内找到匹配项。
      返回:
      此匹配器区域的结束点
      自版本:
      1.5
    • hasTransparentBounds

      public boolean hasTransparentBounds()
      查询此匹配器的区域边界透明度。

      如果此匹配器使用透明边界,则此方法返回true,如果使用不透明边界则返回false

      请参阅useTransparentBounds以了解透明和不透明边界的描述。

      默认情况下,匹配器使用不透明的区域边界。

      返回:
      如果此匹配器使用透明边界,则返回true,否则返回false
      自版本:
      1.5
      另请参阅:
    • useTransparentBounds

      public Matcher useTransparentBounds(boolean b)
      设置此匹配器的区域边界的透明度。

      使用参数true调用此方法将使此匹配器使用透明边界。如果布尔参数为false,则将使用不透明边界。

      使用透明边界,此匹配器的区域边界对于预查、回顾和边界匹配构造是透明的。这些构造可以查看区域边界之外以确定是否匹配适当。

      使用不透明边界,此匹配器的区域边界对于可能尝试查看其之外的预查、回顾和边界匹配构造是不透明的。这些构造无法查看边界之外,因此它们将无法匹配区域之外的任何内容。

      默认情况下,匹配器使用不透明边界。

      参数:
      b - 一个布尔值,指示是否使用不透明或透明区域
      返回:
      此匹配器
      自版本:
      1.5
      另请参阅:
    • hasAnchoringBounds

      public boolean hasAnchoringBounds()
      查询此匹配器的区域边界锚定。

      如果此匹配器使用锚定边界,则此方法返回true,否则返回false

      请参阅useAnchoringBounds以了解锚定边界的描述。

      默认情况下,匹配器使用锚定区域边界。

      返回:
      如果此匹配器使用锚定边界,则返回true,否则返回false
      自版本:
      1.5
      另请参阅:
    • useAnchoringBounds

      public Matcher useAnchoringBounds(boolean b)
      设置此匹配器的区域边界锚定。

      使用参数true调用此方法将使此匹配器使用锚定边界。如果布尔参数为false,则将使用非锚定边界。

      使用锚定边界,此匹配器的区域边界匹配锚点(如^和$)。

      没有锚定边界时,此匹配器的区域边界将不匹配锚点(如^和$)。

      默认情况下,匹配器使用锚定区域边界。

      参数:
      b - 一个布尔值,指示是否使用锚定边界。
      返回:
      此匹配器
      自版本:
      1.5
      另请参阅:
    • toString

      public String toString()

      返回此匹配器的字符串表示形式。 Matcher的字符串表示形式包含可能对调试有用的信息。确切的格式未指定。

      覆盖:
      toString 在类 Object
      返回:
      此匹配器的字符串表示形式
      自版本:
      1.5
    • hitEnd

      public boolean hitEnd()

      如果搜索引擎在此匹配器执行的最后一次匹配操作中命中输入的结尾,则返回true。

      当此方法返回true时,可能会有更多的输入会改变上次搜索的结果。

      返回:
      如果上次匹配中命中输入的结尾,则返回true;否则返回false
      自版本:
      1.5
    • requireEnd

      public boolean requireEnd()

      如果更多的输入可能会将正匹配变为负匹配,则返回true。

      如果此方法返回true,并且找到了匹配项,则更多的输入可能导致匹配丢失。如果此方法返回false并且找到了匹配项,则更多的输入可能会改变匹配但匹配不会丢失。如果未找到匹配项,则requireEnd没有意义。

      返回:
      如果更多的输入可能会将正匹配变为负匹配,则返回true。
      自版本:
      1.5
    • namedGroups

      public Map<String,Integer> namedGroups()
      返回从捕获组名称到组编号的不可修改映射。如果没有命名组,则返回一个空映射。
      指定者:
      namedGroups 在接口 MatchResult
      返回:
      从捕获组名称到组编号的不可修改映射
      自版本:
      20
    • hasMatch

      public boolean hasMatch()
      返回this是否包含来自先前匹配或查找操作的有效匹配。
      指定者:
      hasMatch 在接口 MatchResult
      返回:
      this是否包含有效匹配
      自版本:
      20