java.time.Matcher.lookingAt()方法

java.time.Matcher.lookingAt()方法嘗試將從區域開頭開始的輸入序列與模式匹配。

聲明

以下是java.time.Matcher.lookingAt()方法的聲明。

public boolean lookingAt()

返回值

當且僅當輸入序列的首碼與此匹配器的模式匹配時才返回true

示例

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

package com.zaixian;

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

public class MatcherDemo {
   private static final String REGEX = "foo";
   private static final String INPUT = "fooooooooooooooooo";
   private static Pattern pattern;
   private static Matcher matcher;

   public static void main( String args[] ) {
      pattern = Pattern.compile(REGEX);
      matcher = pattern.matcher(INPUT);

      System.out.println("Current REGEX is: "+REGEX);
      System.out.println("Current INPUT is: "+INPUT);

      System.out.println("lookingAt(): "+matcher.lookingAt());
      System.out.println("matches(): "+matcher.matches());
   }
}

編譯並運行上面的程式,這將產生以下結果 -

Current REGEX is: foo
Current INPUT is: fooooooooooooooooo
lookingAt(): true
matches(): false

上一篇: Java正則運算式Matcher類 下一篇: Java正則運算式PatternSyntaxException類