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

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

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

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

উদাহরণ ১

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

public class RegionStartExample {
    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("Starting of the region: " + matcher.regionStart());
    }
}

আউটপুট

Enter a string: #sample text
Match not found
Starting of the region: 2

উদাহরণ ২

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

public class RegionStartExample {
    public static void main(String[] args) {
        String regex = "(.*)(\\d+)(.*)";
        String input = "124 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("Start of the region: " + matcher.regionStart());
    }
}

আউটপুট

Match not found
Start of the region: 5
  1. জাভাতে Pattern pattern() মেথড - উদাহরণ সহ ব্যাখ্যা

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

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

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