কম্পিউটার টিউটোরিয়াল

জাভায় Matcher hasTransparentBounds() মেথড উদাহরণসহ ব্যাখ্যা

java.util.regex.Matcher ক্লাস বিভিন্ন ম্যাচ অপারেশন সম্পাদন করার একটি ইঞ্জিন প্রতিনিধিত্ব করে। এই ক্লাসের কোনো কনস্ট্রাক্টর নেই, আপনি java.util.regex.Pattern ক্লাসের matches() মেথড ব্যবহার করে এই ক্লাসের অবজেক্ট তৈরি/প্রाप्त করতে পারেন।

রেগুলার এক্সপ্রেশন-এ, lookbehind এবং lookahead কনস্ট্রাক্টগুলো একটি নির্দিষ্ট প্যাটার্ন মিলাতে ব্যবহৃত হয় যা অন্য প্যাটার্নের পূর্বে বা পরে থাকে। উদাহরণস্বরূপ, আপনি যদি ৫ থেকে ১২ ক্যারেক্টার গ্রহণকারী একটি স্ট্রিং চান তবে রেগুলার এক্সপ্রেশন হবে:

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

ডিফল্টভাবে, ম্যাচার অঞ্চলের সীমানা lookahead, lookbehind এবং বাউন্ডারি ম্যাচিং কনস্ট্রাক্টগুলোর জন্য ট্রান্সপারেন্ট নয় অর্থাৎ এই কনস্ট্রাক্টগুলো ইনপুট টেক্সটের অঞ্চলের সীমানা বাইরের কন্টেন্ট মিলাতে পারে না।

hasTransparentBounds() মেথড যাচাই করে যে বর্তমান ম্যাচার ট্রান্সপারেন্ট বাউন্ডস ব্যবহার করছে কিনা, যদি হয় তবে true ফেরত দেয় অন্যথায় false।

উদাহরণ ১

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HasTransparentBounds {
    public static void main(String[] args) {
        // ৬ থেকে ১০ ক্যারেক্টার গ্রহণের রেগুলার এক্সপ্রেশন
        String regex = "\\A(?=\\w{6,10}\\z)";
        System.out.println("Enter 5 to 12 characters: ");
        String input = new Scanner(System.in).next();
        
        // Pattern অবজেক্ট তৈরি
        Pattern pattern = Pattern.compile(regex);
        
        // Matcher অবজেক্ট তৈরি
        Matcher matcher = pattern.matcher(input);
        
        // ইনপুট স্ট্রিং-এ রিজন সেট করা
        matcher.region(0, 4);
        
        if(matcher.find()) {
            System.out.println("Match found");
        } else {
            System.out.println("Match not found");
        }
        
        boolean bool = matcher.hasTransparentBounds();
        
        // ট্রান্সপারেন্ট বাউন্ডসে স্যুইচ করা
        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

উদাহরণ ২

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HasTransparentBounds {
    public static void main(String[] args) {
        // ৬ থেকে ১০ ক্যারেক্টার গ্রহণের রেগুলার এক্সপ্রেশন
        String regex = "\\A(?=\\w{6,10}\\z)";
        System.out.println("Enter 5 to 12 characters: ");
        String input = new Scanner(System.in).next();
        
        // Pattern অবজেক্ট তৈরি
        Pattern pattern = Pattern.compile(regex);
        
        // Matcher অবজেক্ট তৈরি
        Matcher matcher = pattern.matcher(input);
        
        // ইনপুট স্ট্রিং-এ রিজন সেট করা
        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();
        
        // ট্রান্সপারেন্ট বাউন্ডসে স্যুইচ করা
        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
  1. জাভাতে Pattern pattern() মেথড - উদাহরণ সহ ব্যাখ্যা

  2. জাভাতে Pattern.matcher() মেথড - উদাহরণ সহ ব্যাখ্যা

  3. জাভাতে Matcher matches() মেথড - উদাহরণ সহ ব্যাখ্যা

  4. জাভাতে Matcher start() মেথড - উদাহরণ সহ ব্যাখ্যা