English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Matcher hasAnchoringBounds() method with example in Java

The java.util.regex.Matcher class represents the engine for executing various matching operations. This class has no constructor, and you can create it using the matchs() method of the class java.util.regex.Pattern/Get an object of this class.

Anchoring bounds are used for area matching, such as ^ and $. By default, the matcher uses anchoring bounds, and you can use the useAnchoringBounds() method to switch from using anchoring bounds to non-anchoring bounds.

This (Matcher) classhasAnchoringBounds()Method verifies whether the current matcher uses anchoring bounds (if so), otherwise returns true, otherwise returns false.

Example1

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HasAnchoringBoundsExample {
   public static void main(String[] args) {
      String regex = "(.*)(\\d+)(.*);
      String input = "This is a sample Text" 1234, with numbers in between.
         + "\nThis is the second line in the text"
         + "\nThis is third line in the text";
      //Creating a pattern object
      Pattern pattern = Pattern.compile(regex);
      //Creating a Matcher object
      Matcher matcher = pattern.matcher(input);
      //Verifying for anchoring bounds
      boolean bool = matcher.hasAnchoringBounds();
      //checking for the match
      if(bool) {
         System.out.println("Current matcher uses anchoring bounds");
      } else {
         System.out.println("Current matcher uses non-anchoring bounds));
      }
      if(matcher.matches()) {
         System.out.println("Match found");
      } else {
         System.out.println("Match not found");
      }
   }
}

Output result

Current matcher uses anchoring bounds
Match not found

Example2

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Trail {
   public static void main(String args[]) {
      //Reading string value
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter input string");
      String input = sc.nextLine();
      //Regular expression to find digits
      String regex = "."*\\d+.*";
      //Compiling the regular expression
      Pattern pattern = Pattern.compile(regex);
      //Printing the regular expression
      System.out.println("Compiled regular expression: "+pattern.toString());
      //Retrieving the matcher object
      Matcher matcher = pattern.matcher(input);
      matcher.useAnchoringBounds(false);
      boolean hasBounds = matcher.hasAnchoringBounds();
      if(hasBounds) {
         System.out.println("Current matcher uses anchoring bounds");
      } else {
         System.out.println("Current matcher uses non-anchoring bounds));
      }
      //verifying whether match occurred
      if(matcher.matches()) {
         System.out.println("Given String contains digits");
      } else {
         System.out.println("Given String does not contain digits");
      }
   }
}

Output result

Enter input string
hello sample 2
Compiled regular expression: .*\d+.*
Current matcher uses non-anchoring bounds
Given String contains digits