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

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

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

regionEnd() মেথড (Matcher ক্লাসের) একটি ইন্টিজার মান ফেরত দেয় যা বর্তমান ম্যাচার অবজেক্টের শেষ ইনডেক্স প্রতিনিধিত্ব করে।

উদাহরণ ১

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

public class RegionEndExample {
    public static void main(String[] args) {
        String regex = "(.*)(\\d+)(.*)";
        String input = "This is a sample Text, 1234, with numbers in between.";
        
        // Pattern অবজেক্ট তৈরি
        Pattern pattern = Pattern.compile(regex);
        
        // Matcher অবজেক্ট তৈরি
        Matcher matcher = pattern.matcher(input);
        
        // ম্যাচারের রিজন সেট করা
        matcher.region(5, 20);
        
        if(matcher.matches()) {
            System.out.println("Match found");
        } else {
            System.out.println("Match not found");
        }
        
        System.out.print("End of the region: " + matcher.regionEnd());
    }
}

আউটপুট

Match not found
End of the region: 20

উদাহরণ ২

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

public class RegionEndExample {
    public static void main(String[] args) {
        // ৬ থেকে ১০ ক্যারেক্টার গ্রহণের রেগুলার এক্সপ্রেশন
        String regex = "[#]";
        System.out.println("Enter a string: ");
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        
        // Pattern অবজেক্ট তৈরি
        Pattern pattern = Pattern.compile(regex);
        
        // Matcher অবজেক্ট তৈরি
        Matcher matcher = pattern.matcher(input);
        
        // ইনপুট স্ট্রিং-এ রিজন সেট করা
        matcher.region(2, 4);
        
        // ট্রান্সপারেন্ট বাউন্ডসে স্যুইচ করা
        if(matcher.find()) {
            System.out.println("Match found");
        } else {
            System.out.println("Match not found");
        }
        
        System.out.println("Ending of the region: " + matcher.regionEnd());
    }
}

আউটপুট

Enter a string: this is sample text #
Match not found
Ending of the region: 4
  1. জাভাতে Pattern pattern() মেথড - উদাহরণ সহ ব্যাখ্যা

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

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

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