java.util.regex.Matcher.useAnchoringBounds()



描述

**java.util.regex.Matcher.useAnchoringBounds(boolean b)** 方法設定此匹配器的區域範圍的錨點。

宣告

以下是 **java.util.regex.Matcher.useAnchoringBounds(boolean b)** 方法的宣告。

public Matcher useAnchoringBounds(boolean b)

引數

  • **b** - 指示是否使用錨定邊界的布林值。

返回值

此匹配器。

示例

以下示例展示瞭如何使用 java.util.regex.Matcher.useAnchoringBounds(boolean b) 方法。

package com.tutorialspoint;

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

public class MatcherDemo {
   private static final String REGEX = "(.*)(\\d+)(.*)";
   private static final String INPUT = "This is a sample Text, 1234, with numbers in between.";

   public static void main(String[] args) {
      // create a pattern
      Pattern pattern = Pattern.compile(REGEX);
      
      // get a matcher object
      Matcher matcher = pattern.matcher(INPUT); 
      matcher.useAnchoringBounds(true);
      System.out.println(matcher.hasAnchoringBounds());
   }
}

我們來編譯並執行上述程式,結果如下 -

true
javaregex_matcher.htm
廣告