Matcher hasTransparentBounds() 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.

In regular expressions, the lookbehind and lookahead constructs are used to match a particular pattern that is preceding or, succeeding in some other pattern. For example, if you need to accept a string which accepts 5 to 12 characters the regular expression will be −

"\A(?=\w{6,10}\z)";

By default, the boundaries of the matcher region are not transparent to the constructs lookahead, lookbehind and, boundary matching i.e. These constructs cannot match contents of the input text beyond the region boundaries −

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

Example 1

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HasTransparentBounds {
   public static void main(String[] args) {
      //Regular expression to accepts 6 to 10 characters
      String regex = "\A(?=\w{6,10}\z)";
      System.out.println("Enter 5 to 12 characters: ");
      String input = new Scanner(System.in).next();
      //Creating a pattern object
      Pattern pattern = Pattern.compile(regex);
      //Creating a Matcher object
      Matcher matcher = pattern.matcher(input);
      //Setting region to the input string
      matcher.region(0, 4);
      if(matcher.find()) {
         System.out.println("Match found");
      } else {
         System.out.println("Match not found");
      }
      boolean bool = matcher.hasTransparentBounds();
      //Switching to transparent bounds
      if(bool) {
         System.out.println("Current matcher uses transparent bounds");
      } else {
         System.out.println("Current matcher user non-transparent bound");
      }
   }
}

Output

Enter 5 to 12 characters:
sampletext
Match not found
Current matcher user non-transparent bound

Example 2

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HasTransparentBounds {
   public static void main(String[] args) {
      //Regular expression to accepts 6 to 10 characters
      String regex = "\A(?=\w{6,10}\z)";
      System.out.println("Enter 5 to 12 characters: ");
      String input = new Scanner(System.in).next();
      //Creating a pattern object
      Pattern pattern = Pattern.compile(regex);
      //Creating a Matcher object
      Matcher matcher = pattern.matcher(input);
      //Setting region to the input string
      matcher.region(0, 4);
      matcher.useTransparentBounds(true);
      if(matcher.find()) {
         System.out.println("Match found");
      } else {
         System.out.println("Match not found");
      }
      boolean bool = matcher.hasTransparentBounds();
      //Switching to transparent bounds
      if(bool) {
         System.out.println("Current matcher uses transparent bounds");
      } else {
         System.out.println("Current matcher user non-transparent bound");
      }
   }
}

Output

Enter 5 to 12 characters:
sampletext
Match found
Current matcher uses transparent bounds

Updated on: 20-Nov-2019

28 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements