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

জাভায় Pattern CASE_INSENSITIVE ফিল্ড উদাহরণসহ ব্যাখ্যা

Pattern ক্লাসের এই CASE_INSENSITIVE ফিল্ড কেস নির্বিশেষে ক্যারেক্টার মিলাতে ব্যবহৃত হয়। আপনি যখন compile() মেথডে এই ফ্ল্যাগ মান হিসেবে ব্যবহার করেন এবং যদি রেগুলার এক্সপ্রেশন ব্যবহার করে ক্যারেক্টার খুঁজেন তবে উভয় কেসের ক্যারেক্টার মিলবে।

দ্রষ্টব্য - ডিফল্টভাবে, এই ফ্ল্যাগ শুধু ASCII ক্যারেক্টার মিলায়।

উদাহরণ ১

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

public class CASE_INSENSITIVE_Example {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter input data: ");
        String input = sc.nextLine();
        System.out.println("Enter required character: ");
        char ch = sc.next().toCharArray()[0];
        
        // প্রয়োজনীয় ক্যারেক্টার খুঁজতে রেগুলার এক্সপ্রেশন
        String regex = "[" + ch + "]";
        
        // রেগুলার এক্সপ্রেশন কম্পাইল
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        
        // Matcher অবজেক্ট পাওয়া
        Matcher matcher = pattern.matcher(input);
        int count = 0;
        
        while (matcher.find()) {
            count++;
        }
        
        System.out.println("The letter " + ch + " occurred " + count + " times in the given text (irrespective of case)");
    }
}

আউটপুট

Enter input data: Tutorials Point originated from the idea that there exists a class of readers who respond better to online content and prefer to learn new skills at their own pace from the comforts of their drawing rooms.
Enter required character: T
The letter T occurred 20 times in the given text (irrespective of case)

উদাহরণ ২

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

public class VerifyBoolean {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a string value: ");
        String str = sc.next();
        
        Pattern pattern = Pattern.compile("true|false", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(str);
        
        if(matcher.matches()) {
            System.out.println("Given string is a boolean type");
        } else {
            System.out.println("Given string is not a boolean type");
        }
    }
}

আউটপুট ১

Enter a string value: true
Given string is a boolean type

আউটপুট ২

Enter a string value: false
Given string is a boolean type

আউটপুট ৩

Enter a string value: hello
Given string is not a boolean type
  1. জাভাতে Pattern.matches() মেথড - উদাহরণ সহ ব্যাখ্যা

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

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

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