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

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

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

এই (Matcher) ক্লাসের reset() মেথড সকল স্টেট ইনফরমেশন সরিয়ে দেয় এবং ক্যারেক্টার সিকোয়েন্সকে ডিফল্টে রিসেট করে এবং অ্যাপেন্ড পজিশনকে শূন্যে নিয়ে আসে।

উদাহরণ ১

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

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

আউটপুট

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

এই মেথডের আরেকটি ভেরিয়েন্ট একটি স্ট্রিং ডেটা গ্রহণ করে এবং সেটি দিয়ে Matcher-কে রিসেট করে।

উদাহরণ ২

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

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

আউটপুট

State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>is</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>example</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>script</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>every</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>word</b>]
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>bold</b>]
State of the matcher after resetting it: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,51 lastmatch=<b>this</b>]
State of the matcher after resetting it: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,51 lastmatch=<b>new</b>]
State of the matcher after resetting it: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,51 lastmatch=<b>after</b>]
  1. জাভায় Matcher toString() মেথড উদাহরণসহ ব্যাখ্যা

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

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

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