java.util.regex.Matcher ক্লাস একটি ইঞ্জিনের প্রতিনিধিত্ব করে যা বিভিন্ন ম্যাচ অপারেশন করে। এই ক্লাসের জন্য কোন কনস্ট্রাক্টর নেই, আপনি java.util.regex.Pattern ক্লাসের matches() পদ্ধতি ব্যবহার করে এই ক্লাসের একটি অবজেক্ট তৈরি/প্রাপ্ত করতে পারেন।
lookingAt() ম্যাচারের পদ্ধতি ক্লাস প্রদত্ত ইনপুট পাঠ্যের সাথে প্যাটার্নের সাথে মেলে, অঞ্চলের শুরু থেকে শুরু করে। একটি ম্যাচের ক্ষেত্রে, এই পদ্ধতিটি সত্য, অন্যথায়, মিথ্যা ফেরত দেয়। matches() পদ্ধতির বিপরীতে এই পদ্ধতিতে সত্য ফিরে আসার জন্য সমগ্র অঞ্চলে একটি মিলের প্রয়োজন হয় না।
উদাহরণ 1
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";
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Creating a Matcher object
Matcher matcher = pattern.matcher(input);
//checking for the match
if(matcher.lookingAt()) {
System.out.println("Match found");
} else {
System.out.println("Match not found");
}
}
} আউটপুট
Match found
উদাহরণ 2
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);
//Regular expression to match a word that contain digits
String regex = ".*\\d+.*";
//Compiling the regular expression
Pattern pattern = Pattern.compile(regex);
//Retrieving the matcher object
Matcher matcher = pattern.matcher(input);
//verifying whether match occurred
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