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

জাভায় Matcher region(int start, int end) মেথড উদাহরণসহ ব্যাখ্যা

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

region() মেথড (Matcher ক্লাসের) দুটি ইন্টিজার মান গ্রহণ করে যা ইনপুট স্ট্রিং-এ পজিশন প্রতিনিধিত্ব করে এবং, বর্তমান ম্যাচারের একটি অঞ্চল সেট করে।

উদাহরণ ১

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

public class RegionExample {
    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");
        }
    }
}

আউটপুট

Enter 5 to 12 characters: sampleText
Match not found

উদাহরণ ২

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

public class RegionExample {
    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(0, 20);
        
        if(matcher.matches()) {
            System.out.println("Match found");
        } else {
            System.out.println("Match not found");
        }
    }
}

আউটপুট

Match not found
  1. জাভায় Matcher replaceAll() মেথড উদাহরণসহ ব্যাখ্যা

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

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

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