java.time.Matcher.matches()範例

2019-10-16 22:19:14

java.time.Matcher.matches()方法嘗試將整個區域與模式匹配。

宣告

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

public boolean matches()

返回值

當且僅當整個區域序列與此匹配器的模式匹配時才為true

範例

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

package com.yiibai;

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