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

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

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

Matcher ক্লাসের usePattern() মেথড একটি Pattern অবজেক্ট গ্রহণ করে যা একটি নতুন রেগেক্স প্যাটার্ন প্রতিনিধিত্ব করে এবং মিল খুঁজে বের করতে এটি ব্যবহার করে।

উদাহরণ

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

public class UsePatternExample {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter input text: ");
        String input = sc.nextLine();
        String regex = "[#%&*]";
        
        // Pattern অবজেক্ট তৈরি
        Pattern pattern = Pattern.compile(regex);
        
        // Matcher অবজেক্ট তৈরি
        Matcher matcher = pattern.matcher(input);
        int count = 0;
        
        while(matcher.find()) {
            count++;
        }
        
        // ব্যবহৃত Pattern পুনরুদ্ধার
        System.out.println("The are " + count + " special characters [# % & *] in the given text");
        
        // ৫ থেকে ৬ ক্যারেক্টার গ্রহণের জন্য প্যাটার্ন তৈরি
        Pattern newPattern = Pattern.compile("\\A(?=\\w{6,15}\\z)");
        
        // নতুন প্যাটার্নে স্যুইচ করা
        matcher = matcher.usePattern(newPattern);
        
        if(matcher.find()) {
            System.out.println("Given input contain 6 to 15 characters");
        } else {
            System.out.println("Given input doesn't contain 6 to 15 characters");
        }
    }
}

আউটপুট

Enter input text: #*mypassword&
The are 3 special characters [# % & *] in the given text
!!mypassword!
Given input doesn't contain 6 to 15 characters
  1. জাভাতে Pattern pattern() মেথড - উদাহরণ সহ ব্যাখ্যা

  2. জাভাতে Pattern.matcher() মেথড - উদাহরণ সহ ব্যাখ্যা

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

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