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

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

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

toMatchResult() মেথড (Matcher ক্লাসের) বর্তমান ম্যাচারের ম্যাচ স্টেট ফেরত দেয়।

উদাহরণ ১

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

public class ToMatchResultExample {
    public static void main(String[] args) {
        String str = "<p>This <b>is</b> an <b>example</b>.</p>";
        
        // বোল্ড ট্যাগের কন্টেন্ট মিলাতে রেগুলার এক্সপ্রেশন
        String regex = "<b>(\\S+)</b>";
        
        // Pattern অবজেক্ট তৈরি
        Pattern pattern = Pattern.compile(regex);
        
        // স্ট্রিং-এ কম্পাইল করা প্যাটার্ন মিলানো
        System.out.println("State of the matcher: ");
        Matcher matcher = pattern.matcher(str);
        
        while (matcher.find()) {
            System.out.println(matcher.toMatchResult());
            String result = matcher.group(1);
        }
        
        matcher = matcher.reset("<p>this is another <b>line</b>.</p>");
        matcher.find();
        System.out.println("");
        System.out.println("State of the matcher after resetting it: \n" + matcher.toMatchResult());
    }
}

আউটপুট

State of the matcher:
java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,40 lastmatch=<b>is</b>]
java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,40 lastmatch=<b>example</b>]
State of the matcher after resetting it:
java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,35 lastmatch=<b>line</b>]

উদাহরণ ২

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

public class ToMatchResultExample {
    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);
        
        System.out.println("Match state: ");
        
        // ম্যাচ খুঁজে বের করা
        while(matcher.find()) {
            System.out.println(matcher.toMatchResult());
        }
    }
}

আউটপুট

Enter a string: #This #is #a #sample #text
Match state:
java.util.regex.Matcher[pattern=[#] region=0,26 lastmatch=#]
java.util.regex.Matcher[pattern=[#] region=0,26 lastmatch=#]
java.util.regex.Matcher[pattern=[#] region=0,26 lastmatch=#]
java.util.regex.Matcher[pattern=[#] region=0,26 lastmatch=#]
java.util.regex.Matcher[pattern=[#] region=0,26 lastmatch=#]
  1. জাভায় Matcher toString() মেথড উদাহরণসহ ব্যাখ্যা

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

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

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