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

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

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

অ্যানকরিং বাউন্ডস (Anchoring bounds) ^ এবং $ এর মতো অঞ্চলের ম্যাচ মিলাতে ব্যবহৃত হয়। ডিফল্টভাবে একটি ম্যাচার অ্যানকরিং বাউন্ডস ব্যবহার করে, আপনি useAnchoringBounds() মেথড ব্যবহার করে অ্যানকরিং বাউন্ডস থেকে নন-অ্যানকরিং বাউন্ডসে স্যুইচ করতে পারেন।

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

উদাহরণ ১

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

public class HasAnchoringBoundsExample {
    public static void main(String[] args) {
        String regex = "(.*)(\\d+)(.*)";
        String input = "This is a sample Text, 1234, with numbers in between. "
                     + "\n This is the second line in the text "
                     + "\n This is third line in the text";
        
        // Pattern অবজেক্ট তৈরি
        Pattern pattern = Pattern.compile(regex);
        
        // Matcher অবজেক্ট তৈরি
        Matcher matcher = pattern.matcher(input);
        
        // অ্যানকরিং বাউন্ডস যাচাই
        boolean bool = matcher.hasAnchoringBounds();
        
        // ম্যাচ যাচাই
        if(bool) {
            System.out.println("Current matcher uses anchoring bounds");
        } else {
            System.out.println("Current matcher uses non-anchoring bounds");
        }
        
        if(matcher.matches()) {
            System.out.println("Match found");
        } else {
            System.out.println("Match not found");
        }
    }
}

আউটপুট

Current matcher uses anchoring bounds
Match not found

উদাহরণ ২

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

public class Trail {
    public static void main(String args[]) {
        // স্ট্রিং মান পড়া
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter input string");
        String input = sc.nextLine();
        
        // অঙ্ক খুঁজতে রেগুলার এক্সপ্রেশন
        String regex = ".*\\d+.*";
        
        // রেগুলার এক্সপ্রেশন কম্পাইল
        Pattern pattern = Pattern.compile(regex);
        
        // কম্পাইল করা রেগুলার এক্সপ্রেশন প্রিন্ট
        System.out.println("Compiled regular expression: " + pattern.toString());
        
        // Matcher অবজেক্ট পাওয়া
        Matcher matcher = pattern.matcher(input);
        matcher.useAnchoringBounds(false);
        boolean hasBounds = matcher.hasAnchoringBounds();
        
        if(hasBounds) {
            System.out.println("Current matcher uses anchoring bounds");
        } else {
            System.out.println("Current matcher uses non-anchoring bounds");
        }
        
        // ম্যাচ হয়েছে কিনা যাচাই
        if(matcher.matches()) {
            System.out.println("Given String contains digits");
        } else {
            System.out.println("Given String does not contain digits");
        }
    }
}

আউটপুট

Enter input string hello sample 2
Compiled regular expression: .*\d+.*
Current matcher uses non-anchoring bounds
Given String contains digits
  1. জাভাতে Pattern pattern() মেথড - উদাহরণ সহ ব্যাখ্যা

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

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

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