English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
java.util.regex.Matcher class represents the engine for executing various matching operations. This class has no constructor, and can be usedmatches()
The method of class java.util.regex.Pattern to create/Get an object of this class.
In regular expressions, lookbehind and lookahead constructs are used to match specific patterns before or after certain other patterns. For example, if you need to accept5to12a string of characters, the regular expression will be-
"\\A(?=\\w{6,10}\\z)";
By default, the boundaries of the matcher's area are not transparent for forward, backward, and boundary matching, that is, these constructs cannot match input text content beyond the area boundaries.-
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class useTransparentBoundsExample { public static void main(String[] args) { //Regular expressions can accept6to10characters 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 the input string matcher.region(0, 4); //Switch to transparent range if(matcher.find()) { System.out.println("Match found"); } else { System.out.println("Match not found"); } } }
Output Result
Enter 5 to 12 characters: sampleText Match not found
This classThe method'suseTransparentBounds()The method accepts a boolean value, if true is passed to this method, then the current matcher will use a transparent range, if false is passed to this method, then a non-transparent range will be used.
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main(String[] args) { //Regular expressions can accept6to10characters 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 the input string matcher.region(0, 4); //Switch to transparent range matcher = matcher.useTransparentBounds(true); if(matcher.find()) { System.out.println("Match found"); } else { System.out.println("Match not found"); } } }
Output Result
Enter 5 to 12 characters: sampletext Match found