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

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

java.util.regex প্যাকেজের Pattern ক্লাস একটি রেগুলার এক্সপ্রেশনের কম্পাইল করা রিপ্রেজেন্টেশন।

এই ক্লাসের toString() মেথড বর্তমান Pattern কম্পাইল করার জন্য ব্যবহৃত রেগুলার এক্সপ্রেশনের স্ট্রিং রিপ্রেজেন্টেশন ফেরত দেয়।

উদাহরণ ১

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

public class Example {
    public static void main(String args[]) {
        // স্ট্রিং মান পড়া
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter input string");
        String input = sc.nextLine();
        
        // অঙ্ক খুঁজতে রেগুলার এক্সপ্রেশন
        String regex = "(\\d)";
        
        // রেগুলার এক্সপ্রেশন কম্পাইল
        Pattern pattern = Pattern.compile(regex);
        
        // রেগুলার এক্সপ্রেশন প্রিন্ট
        System.out.println("Compiled regular expression: " + pattern.toString());
        
        // ম্যাচ হয়েছে কিনা যাচাই
        if(pattern.matcher(input).find())
            System.out.println("Given String contains digits");
        else
            System.out.println("Given String does not contain digits");
    }
}

আউটপুট

Enter input string This 7est contain5 di9its in place of certain charac7er5
Compiled regular expression: (\d)
Given String contains digits

উদাহরণ ২

import java.util.regex.Pattern;

public class Example {
    public static void main(String args[]) {
        String regex = "Tutorialspoint$";
        String input = "Hi how are you welcome to Tutorialspoint";
        
        Pattern pattern = Pattern.compile(regex);
        Matcher match = pattern.matcher(input);
        int count = 0;
        
        if(match.find())
            System.out.println("Match found");
        else
            System.out.println("Match not found");
        
        System.out.println("regular expression: " + pattern.toString());
    }
}

আউটপুট

Match found
regular expression: Tutorialspoint$
  1. জাভাতে Pattern.matches() মেথড - উদাহরণ সহ ব্যাখ্যা

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

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

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