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

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

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

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

উদাহরণ ১

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

public class ToStringExample {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter input text: ");
        String input = sc.nextLine();
        String regex = "[#%&*]";
        
        // Pattern অবজেক্ট তৈরি
        Pattern pattern = Pattern.compile(regex);
        
        // Matcher অবজেক্ট তৈরি
        Matcher matcher = pattern.matcher(input);
        int count = 0;
        
        while(matcher.find()) {
            count++;
        }
        
        // ব্যবহৃত Pattern পুনরুদ্ধার
        System.out.println("The are " + count + " special [# % & *] characters in the given text");
        System.out.println("Following is the string format of the matcher used: \n" + matcher.toString());
    }
}

আউটপুট

Enter input text: Hello# How # are# you *& welcome to T#utorials%point
The are 7 special [# % & *] characters in the given text
Following is the string format of the matcher used:
java.util.regex.Matcher[pattern=[#%&*] region=0,52 lastmatch=]

উদাহরণ ২

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

public class ToStringExample {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter input text: ");
        String input = sc.nextLine();
        String regex = "[#%&*]";
        
        // Pattern অবজেক্ট তৈরি
        Pattern pattern = Pattern.compile(regex);
        
        // Matcher অবজেক্ট তৈরি
        Matcher matcher = pattern.matcher(input);
        int count = 0;
        
        while(matcher.find()) {
            count++;
        }
        
        // ব্যবহৃত Pattern পুনরুদ্ধার
        System.out.println("The are " + count + " special [# % & *] characters in the given text");
        System.out.println("Following is the string format of the matcher used: \n" + matcher.toString());
    }
}

আউটপুট

Enter input text: Hello# How # are# you *& welcome to T#utorials%point
The are 7 special [# % & *] characters in the given text
Following is the string format of the matcher used:
java.util.regex.Matcher[pattern=[#%&*] region=0,52 lastmatch=]
  1. জাভাতে Pattern pattern() মেথড - উদাহরণ সহ ব্যাখ্যা

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

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

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