java.regex এর প্যাটার্ন ক্লাস প্যাকেজ হল একটি রেগুলার এক্সপ্রেশনের একটি সংকলিত উপস্থাপনা৷
কম্পাইল() এই শ্রেণীর পদ্ধতি একটি রেগুলার এক্সপ্রেশনের প্রতিনিধিত্বকারী একটি স্ট্রিং মান গ্রহণ করে এবং একটি প্যাটার্ন অবজেক্ট প্রদান করে।
উদাহরণ
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CompileExample {
public static void main( String args[] ) {
//Reading string value
Scanner sc = new Scanner(System.in);
System.out.println("Enter input string");
String input = sc.nextLine();
//Regular expression to find digits
String regex = "(\\d)";
//Compiling the regular expression
Pattern pattern = Pattern.compile(regex);
//Printing the regular expression
System.out.println("Compiled regular expression: "+pattern.toString());
//Retrieving the matcher object
Matcher matcher = pattern.matcher(input);
//verifying whether match occurred
if(matcher.find()) {
System.out.println("Given String contains digits");
} else {
System.out.println("Given String does not contain digits");
}
}
} আউটপুট
Enter input string hello my id is 1120KKA Compiled regular expression: (\d) Given String contains digits
এই পদ্ধতির আরেকটি বৈকল্পিক পতাকাগুলির প্রতিনিধিত্বকারী একটি পূর্ণসংখ্যা মান গ্রহণ করে, যেখানে প্রতিটি পতাকা একটি ঐচ্ছিক শর্ত নির্দিষ্ট করে, উদাহরণস্বরূপ, CASE_INSENSITIVE রেগুলার এক্সপ্রেশন কম্পাইল করার সময় কেসটিকে উপেক্ষা করে৷
উদাহরণ
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CompileExample {
public static void main( String args[] ) {
//Compiling the regular expression
Pattern pattern = Pattern.compile("[t]", Pattern.CASE_INSENSITIVE);
//Retrieving the matcher object
Matcher matcher = pattern.matcher("Tutorialspoint");
int count = 0;
while(matcher.find()) {
count++;
}
System.out.println("Number of matches: "+count);
}
} আউটপুট
Enter input string Tutorialspoint Number of matches: 3