কম্পিউটার

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


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

toMatchResult() এর পদ্ধতি (Matcher) বর্তমান ম্যাচারের ম্যাচের অবস্থা ফিরিয়ে দেয়।

উদাহরণ 1

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>";
      //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
      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>]

উদাহরণ 2

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();
      //Creating a pattern object
      Pattern pattern = Pattern.compile(regex);
      //Creating a Matcher object
      Matcher matcher = pattern.matcher(input);
      System.out.println("Match state: ");
      //Finding the match
      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. উদাহরণ সহ জাভাতে ম্যাচার টু স্ট্রিং() পদ্ধতি

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

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

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