java.util.regex.Matcher.hitEnd() 方法



描述

java.time.Matcher.hitEnd() 方法如果輸入的末尾在這個匹配器執行的最近的匹配操作中被搜尋引擎命中,則返回 true。

宣告

以下是 java.time.Matcher.hitEnd() 方法的宣告。

public boolean hitEnd()

返回值

如果最近的匹配中命中了輸入末尾,則為 true;否則為 false

示例

以下示例展示了 java.time.Matcher.hitEnd() 方法的用法。

package com.tutorialspoint;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MatcherDemo {
   private static String REGEX = "(a*b)(foo)";
   private static String INPUT = "aabfooaabfooabfoob";
   private static String REPLACE = "-";
   
   public static void main(String[] args) {
      Pattern pattern = Pattern.compile(REGEX);
      
      // get a matcher object
      Matcher matcher = pattern.matcher(INPUT);
      
      while(matcher.find()) {
         //Prints the offset after the last character matched.
         System.out.println("First Capturing Group, (a*b) Match String end(): "+matcher.end());    
      }
      System.out.println("hitEnd(): "+matcher.hitEnd());    
   }
}

讓我們編譯並執行以上程式,這將生成以下結果 −

First Capturing Group, (a*b) Match String end(): 6
First Capturing Group, (a*b) Match String end(): 12
First Capturing Group, (a*b) Match String end(): 17
hitEnd(): true
javaregex_matcher.htm
廣告
© . All rights reserved.