কম্পিউটার

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


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

প্যাটার্ন() এর পদ্ধতি (ম্যাচার ) শ্রেণী বর্তমান ম্যাচার দ্বারা ব্যাখ্যা করা একটি প্যাটার্ন (অবজেক্ট) নিয়ে আসে এবং ফেরত দেয়।

উদাহরণ 1

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PatternExample {
   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter your date of birth (MM/DD/YYY)");
      String date = sc.next();
      String regex = "^(1[0-2]|0[1-9])/(3[01]|[12][0-9]|0[1-9])/[0-9]{4}$";
      //Creating a pattern object
      Pattern pattern = Pattern.compile(regex);
      //Creating a Matcher object
      Matcher matcher = pattern.matcher(date);
      //Validating the date
      if(matcher.matches())
         System.out.println("Date is valid");
      else
         System.out.println("Date is not valid");
      //Retrieving Pattern used
      Pattern p = matcher.pattern();
      System.out.println("Pattern used to match the given date: \n"+p);
   }
}

আউটপুট

Enter your date of birth
01/21/2019
Date is valid
Pattern used to match the given date:
^(1[0-2]|0[1-9])/(3[01]|[12][0-9]|0[1-9])/[0-9]{4}$

উদাহরণ 2

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PatternExample {
   public static void main(String args[]) {
      //Reading String from user
      System.out.println("Enter a String");
      Scanner sc = new Scanner(System.in);
      String input = sc.next();
      //Regular expression to match word that starts with a digit
      String regex = "^[0-9].*$";
      //Compiling the regular expression
      Pattern pattern = Pattern.compile(regex);
      //Retrieving the matcher object
      Matcher matcher = pattern.matcher(input);
      Pattern p = matcher.pattern();
      System.out.println("Pattern used to match the given input string: "+p);
      //verifying whether match occurred
      if(matcher.matches())
         System.out.println("First character is a digit");
      else
         System.out.println("First character is not a digit");
   }
}

আউটপুট

Enter a String
2sample
Pattern used to match the given input string: ^[0-9].*$
First character is a digit

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

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

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

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