কম্পিউটার

উদাহরণ সহ জাভাতে ম্যাচার রিসেট() পদ্ধতি


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

এই (Matcher) ক্লাসের reset() পদ্ধতিটি সমস্ত রাজ্যের তথ্য সরিয়ে দেয় এবং অক্ষর ক্রমটিকে ডিফল্টে পুনরায় সেট করে এবং শূন্যে অবস্থান যুক্ত করে৷

উদাহরণ1

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>.";
      //Regular expression to match contents of the bold tags
      String regex = "<b>(\\S+)</b>";
      //Creating a pattern object
      Pattern pattern = Pattern.compile(regex);
      //Matching the compiled pattern in the String
      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=]

এই পদ্ধতির আরেকটি রূপ একটি স্ট্রিং ডেটা গ্রহণ করে এবং এটির সাথে ম্যাচার রিসেট করে।

উদাহরণ 2

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>.";
      //Regular expression to match contents of the bold tags
      String regex = "(\\S+)";
      //Creating a pattern object
      Pattern pattern = Pattern.compile(regex);
      //Matching the compiled pattern in the String
      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. উদাহরণ সহ জাভাতে ম্যাচার টু স্ট্রিং() পদ্ধতি

  2. জাভাতে ম্যাচার রিপ্লেস ফার্স্ট() পদ্ধতি উদাহরণ সহ

  3. জাভাতে Matcher ReplaceAll() পদ্ধতি উদাহরণ সহ

  4. উদাহরণ সহ জাভাতে ম্যাচার প্যাটার্ন() পদ্ধতি