বা লজিক্যাল অপারেটর ব্যবহার করে | জাভা রেগুলার এক্সপ্রেশনের আপনি দুটি প্রদত্ত এক্সপ্রেশনের যেকোনো একটির সাথে মিলতে পারেন।
উদাহরণস্বরূপ, যদি আপনার প্রয়োজন হয় যে আপনার রেগুলার এক্সপ্রেশনটি একাধিক এক্সপ্রেশনের সাথে মিলিত হোক আপনি প্রয়োজনীয় এক্সপ্রেশনগুলিকে “|” দ্বারা আলাদা করে তা করতে পারেন।
উদাহরণ 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 string that starts with hello or ends with bye
String regex = "^hello|bye$";
//Compiling the regular expression
Pattern pattern = Pattern.compile(regex);
//Retrieving the matcher object
Matcher matcher = pattern.matcher(input);
if(matcher.find()) {
System.out.println("Match occurred");
} else {
System.out.println("Match not occurred");
}
}
} আউটপুট 1
Enter a String hello how are you Match occurred
আউটপুট 2
Enter a String This is a sample string Match not occurred
উদাহরণ 2
import java.util.Scanner;
public class RegexExample {
public static void main( String args[] ) {
//Regular expression to match either yes or no String regex = "yes|no";
System.out.println("Enter input value: ");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
boolean bool = input.matches(regex);
if(bool) {
System.out.println("match occurred");
} else {
System.out.println("match not accepted");
}
}
} আউটপুট 1
Enter input value: yes match occurred
আউটপুট 2
Enter input value: hello match not accepted