java.util.regex.Matcher ক্লাস বিভিন্ন ম্যাচ অপারেশন সম্পাদন করার একটি ইঞ্জিন প্রতিনিধিত্ব করে। এই ক্লাসের কোনো কনস্ট্রাক্টর নেই, আপনি java.util.regex.Pattern ক্লাসের matches() মেথড ব্যবহার করে এই ক্লাসের অবজেক্ট তৈরি/প্রाप्त করতে পারেন।
ম্যাচ হলে, এই (Matcher) ক্লাসের requireEnd() মেথড যাচাই করে যে আরও ইনপুট থাকলে ম্যাচ ফলাফল false হতে পারে কিনা, যদি সম্ভাবনা থাকে তবে এটি true ফেরত দেয় অন্যথায় false।
উদাহরণস্বরূপ, আপনি যদি ইনপুট স্ট্রিং-এর শেষ শব্দটি "you" কিনা তা যাচাই করতে রেগেক্স "you$" ব্যবহার করেন এবং আপনার প্রথম ইনপুট লাইন হয় "hello how are you" তবে আপনার ম্যাচ পাওয়া যেতে পারে, কিন্তু আপনি আরও বাক্য গ্রহণ করলে নতুন লাইন(গুলো)의 শেষ শব্দটি প্রয়োজনীয় শব্দ (যেটি "you") নাও হতে পারে, ফলে আপনার ম্যাচ ফলাফল false হয়ে যাবে। এই ধরনের ক্ষেত্রে, requiredEnd() মেথড true ফেরত দেয়।
একইভাবে, আপনি যদি ইনপুট-এ একটি নির্দিষ্ট ক্যারেক্টার (#) মিলাতে চান এবং আপনার প্রথম ইনপুট লাইন হয় "Hello # how are you" তবে আপনি একটি ম্যাচ পাবেন এবং, আরও ইনপুট ডেটা ম্যাচারের কন্টেন্ট পরিবর্তন করতে পারে কিন্তু ফলাফল যা true সেটি পরিবর্তন করবে না। এই পরিস্থিতিতে requiredEnd() মেথড false ফেরত দেয়।
উদাহরণ ১
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RequiredEndExample {
public static void main(String args[]) {
String regex = "you$";
// ব্যবহারকারীর কাছ থেকে ইনপুট পড়া
Scanner sc = new Scanner(System.in);
System.out.println("Enter input text: ");
String input = sc.nextLine();
// Pattern ক্লাস ইনস্ট্যানশিয়েট
Pattern pattern = Pattern.compile(regex);
// Matcher ক্লাস ইনস্ট্যানশিয়েট
Matcher matcher = pattern.matcher(input);
// ম্যাচ হয়েছে কিনা যাচাই
if(matcher.find()) {
System.out.println("Match found");
}
boolean result = matcher.requireEnd();
if(result) {
System.out.println("More input may turn the result of the match false");
} else {
System.out.println("The result of the match will be true, inspite of more data");
}
}
}
আউটপুট
Enter input text: Hello how are you Match found More input may turn the result of the match false
উদাহরণ ২
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RequiredEndExample {
public static void main(String args[]) {
String regex = "[#]";
// ব্যবহারকারীর কাছ থেকে ইনপুট পড়া
Scanner sc = new Scanner(System.in);
System.out.println("Enter input text: ");
String input = sc.nextLine();
// Pattern ক্লাস ইনস্ট্যানশিয়েট
Pattern pattern = Pattern.compile(regex);
// Matcher ক্লাস ইনস্ট্যানশিয়েট
Matcher matcher = pattern.matcher(input);
// ম্যাচ হয়েছে কিনা যাচাই
if(matcher.find()) {
System.out.println("Match found");
}
boolean result = matcher.requireEnd();
if(result) {
System.out.println("More input may turn the result of the match false");
} else {
System.out.println("The result of the match will be true, in spite of more data");
}
}
}
আউটপুট
Enter input text: Hello# how# are you Match found The result of the match will be true, in spite of more data