মেটা অক্ষর "।" জাভা রেগুলার এক্সপ্রেশন যেকোন অক্ষর (একক) এর সাথে মেলে এটি বর্ণমালা, সংখ্যা বা যে কোন বিশেষ অক্ষর হতে পারে।
উদাহরণ 1
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String args[]) {
//Reading String from user
System.out.println("Enter a String");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
//Regular expression to match any character
String regex = ".";
//Compiling the regular expression
Pattern pattern = Pattern.compile(regex);
//Retrieving the matcher object
Matcher matcher = pattern.matcher(input);
int count = 0;
while(matcher.find()) {
count ++;
}
System.out.println("Given string contains "+count+" characters.");
}
} আউটপুট
Enter a String hello how are you welcome to tutorialspoint Given string contains 42 characters.
আপনি নিম্নলিখিত রেগুলার এক্সপ্রেশন −
ব্যবহার করে a এবং b এর মধ্যে যেকোন 3টি অক্ষর মেলাতে পারেনa…b
একইভাবে ".*" অভিব্যক্তিটি n অক্ষরের সাথে মেলে।
উদাহরণ 2
নিম্নলিখিত জাভা প্রোগ্রামটি ব্যবহারকারীর কাছ থেকে 5টি স্ট্রিং পড়ে এবং সেগুলিকে গ্রহণ করে যা b দিয়ে শুরু হয় এবং তাদের মধ্যে যে কোনও অক্ষর সহ a দিয়ে শেষ হয়৷
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
public static void main( String args[] ) {
String regex = "^b.*a$";
Scanner sc = new Scanner(System.in);
System.out.println("Enter 5 input strings: ");
String input[] = new String[5];
for (int i=0; i<5; i++) {
input[i] = sc.nextLine();
}
//Creating a Pattern object
Pattern p = Pattern.compile(regex);
for(int i=0; i<5;i++) {
//Creating a Matcher object
Matcher m = p.matcher(input[i]);
if(m.find()) {
System.out.println(input[i]+": accepted");
} else {
System.out.println(input[i]+": not accepted");
}
}
}
} আউটপুট
Enter 5 input strings: barbara boolean baroda ram raju barbara: accepted boolean: not accepted baroda: accepted ram: not accepted raju: not accepted