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

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

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

lookingAt() মেথড (Matcher ক্লাসের) দেওয়া ইনপুট টেক্সটকে প্যাটার্নের সাথে মিলায়, অঞ্চলের শুরু থেকে। ম্যাচ পেলে এই মেথড true ফেরত দেয়, অন্যথায় false। matches() মেথডের विपরীতে, এই মেথড পুরো অঞ্চলে ম্যাচ পাওয়ার জন্য বাধ্য নয় true ফেরত দেয়ার জন্য।

উদাহরণ ১

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

public class Test {
    public static void main(String[] args) {
        String regex = "(.*)(\\d+)(.*)";
        String input = "This is a sample Text, 1234, with numbers in between. "
                     + "\n This is the second line in the text "
                     + "\n This is third line in the text";
        
        // Pattern অবজেক্ট তৈরি
        Pattern pattern = Pattern.compile(regex);
        
        // Matcher অবজেক্ট তৈরি
        Matcher matcher = pattern.matcher(input);
        
        // ম্যাচ যাচাই
        if(matcher.lookingAt()) {
            System.out.println("Match found");
        } else {
            System.out.println("Match not found");
        }
    }
}

আউটপুট

Match found

উদাহরণ ২

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

public class LookingAtExample {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter String1: ");
        String input1 = sc.nextLine();
        System.out.println("Enter String2: ");
        String input2 = sc.nextLine();
        System.out.println("Enter String3: ");
        String input3 = sc.nextLine();
        String input = input1 + "\n" + input2 + "\n" + input3;
        System.out.println(input);
        
        // অঙ্ক ধারণকারী শব্দ মিলাতে রেগুলার এক্সপ্রেশন
        String regex = ".*\\d+.*";
        
        // রেগুলার এক্সপ্রেশন কম্পাইল
        Pattern pattern = Pattern.compile(regex);
        
        // Matcher অবজেক্ট পাওয়া
        Matcher matcher = pattern.matcher(input);
        
        // ম্যাচ হয়েছে কিনা যাচাই
        boolean bool = matcher.lookingAt();
        
        if(bool) {
            System.out.println("Given input contains digit");
        } else {
            System.out.println("Given input does not contain any digit");
        }
    }
}

আউটপুট

Enter String1: sample text2
Enter String2: data
Enter String3: sample
sample text2
data
sample
Given input contains digit
  1. জাভাতে Pattern pattern() মেথড - উদাহরণ সহ ব্যাখ্যা

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

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

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