java.util.regex.Matcher ক্লাস একটি ইঞ্জিনের প্রতিনিধিত্ব করে যা বিভিন্ন ম্যাচ অপারেশন করে। এই ক্লাসের জন্য কোন কনস্ট্রাক্টর নেই, আপনি java.util.regex.Pattern ক্লাসের matches() পদ্ধতি ব্যবহার করে এই ক্লাসের একটি অবজেক্ট তৈরি/প্রাপ্ত করতে পারেন।
রেগুলার এক্সপ্রেশনে, লুক বিহাইন্ড এবং লুকআহেড কনস্ট্রাক্টগুলি একটি নির্দিষ্ট প্যাটার্নের সাথে মেলে যা পূর্ববর্তী বা অন্য কোনো প্যাটার্নে সফল হয়। উদাহরণস্বরূপ, যদি আপনাকে একটি স্ট্রিং গ্রহণ করতে হয় যা 5 থেকে 12টি অক্ষর গ্রহণ করে তবে রেগুলার এক্সপ্রেশন হবে −
"\\A(?=\\w{6,10}\\z)"; ডিফল্টরূপে, ম্যাচার অঞ্চলের সীমানাগুলি সামনের দিকে, পিছনের দিকে এবং সীমানা মিলে যাওয়া নির্মাণগুলির জন্য স্বচ্ছ নয়, যেমন এই নির্মাণগুলি অঞ্চলের সীমানা অতিক্রম করে ইনপুট পাঠ্যের বিষয়বস্তুর সাথে মেলে না -
hasTransparentBounds() এই ক্লাস পদ্ধতির পদ্ধতিটি যাচাই করে যে বর্তমান ম্যাচার স্বচ্ছ সীমানা ব্যবহার করে যদি তাই হয় তবে এটি সত্য ফেরত দেয়, অন্যথায় এটি মিথ্যা ফেরত দেয়।
উদাহরণ 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");
}
}
} আউটপুট
Enter 5 to 12 characters: sampletext Match not found Current matcher user non-transparent bound
উদাহরণ 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");
}
}
} আউটপুট
Enter 5 to 12 characters: sampletext Match found Current matcher uses transparent bounds