Matcher hasAnchoringBounds() method in Java with Examples


The java.util.regex.Matcher class represents an engine that performs various match operations. There is no constructor for this class, you can create/obtain an object of this class using the matches() method of the class java.util.regex.Pattern.

The anchoring bounds are used to match the region matches such as ^ and $. By default a matcher uses anchoring bounds, you can switch from using anchoring bounds to non-anchoring bounds using the useAnchoringBounds() method.

The hasAnchoringBounds() method of this (Matcher) class verifies whether the current matcher uses anchoring bounds if so, it returns true else, it returns false.

Example 1

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. "
         + "\n This is the second line in the text "
         + "\n This 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

Current matcher uses anchoring bounds
Match not found

Example 2

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

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

Updated on: 20-Nov-2019

53 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements