java.util.regex.Matcher ক্লাস একটি ইঞ্জিনের প্রতিনিধিত্ব করে যা বিভিন্ন ম্যাচ অপারেশন করে। এই ক্লাসের জন্য কোন কনস্ট্রাক্টর নেই, আপনি java.util.regex.Pattern ক্লাসের matches() পদ্ধতি ব্যবহার করে এই ক্লাসের একটি অবজেক্ট তৈরি/প্রাপ্ত করতে পারেন।
usePattern() ম্যাচার ক্লাসের পদ্ধতিটি একটি নতুন রেজেক্স প্যাটার্নের প্রতিনিধিত্বকারী একটি প্যাটার্ন অবজেক্ট গ্রহণ করে এবং মিলগুলি খুঁজে পেতে এটি ব্যবহার করে৷
উদাহরণ
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 = "[#%&*]";
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Creating a Matcher object
Matcher matcher = pattern.matcher(input);
int count =0;
while(matcher.find()) {
count++;
}
//Retrieving Pattern used
System.out.println("The are "+count+" special characters [# % & *] in the given text");
//Building a pattern to accept 5 t 6 characters
Pattern newPattern = Pattern.compile("\\A(?=\\w{6,15}\\z)");
//Switching to new pattern
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