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

Java's Matcher hasTransparentBounds() method with examples

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

In regular expressions, lookbehind and lookahead constructs are used to match specific patterns before or after some other patterns. For example, if you need to accept5to12a string of characters, then the regular expression is-

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

By default, the boundaries of the matching area are not transparent for forward, backward, and boundary matching structures, that is, these structures cannot match input text content outside the area boundaries-

This classof the methodhasTransparentBounds()The method verifies whether the current matcher uses transparent boundaries, if so, returns true, otherwise returns false.

Example1

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HasTransparentBounds {
   public static void main(String[] args) {
      //The regular expression can accept6to10a character
      String regex = "\\A(?=\\w{6,10}"\\z)";
      System.out.println("Enter 5 to 12 characters: ");
      String input = new Scanner(System.in).next();
      //Create a pattern object
      Pattern pattern = Pattern.compile(regex);
      //Create a Matcher object
      Matcher matcher = pattern.matcher(input);
      //Set the region to 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();
      //Switch to transparent range
      if(bool) {
         System.out.println("Current matcher uses transparent bounds");
      } else {
         System.out.println("Current matcher user non-transparent bound");
      }
   }
}

Output Result

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

Example2

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HasTransparentBounds {
   public static void main(String[] args) {
      //The regular expression can accept6to10a character
      String regex = "\\A(?=\\w{6,10}"\\z)";
      System.out.println("Enter 5 to 12 characters: ");
      String input = new Scanner(System.in).next();
      //Create a pattern object
      Pattern pattern = Pattern.compile(regex);
      //Create a Matcher object
      Matcher matcher = pattern.matcher(input);
      //Set the region to 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();
      //Switch to transparent range
      if(bool) {
         System.out.println("Current matcher uses transparent bounds");
      } else {
         System.out.println("Current matcher user non-transparent bound");
      }
   }
}

Output Result

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