কম্পিউটার

জাভাতে রেগুলার এক্সপ্রেশন a|b মেটাক্যারেক্টার


সাব এক্সপ্রেশন/মেটাচ্যারেক্টার “a| b ” হয় a বা b.

মেলে

উদাহরণ 1

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
   public static void main( String args[] ) {
      String regex = "Hello|welcome";
      String input = "Hello how are you welcome to Tutorialspoint";
      Pattern p = Pattern.compile(regex);
      Matcher m = p.matcher(input);
      int count = 0;
      while(m.find()) {
         count++;
      }
      System.out.println("Number of matches: "+count);
   }
}

আউটপুট

Number of matches: 2

উদাহরণ 2

নিম্নলিখিত জাভা প্রোগ্রামটি ব্যবহারকারীর কাছ থেকে লিঙ্গ মান পড়ে এবং এটি শুধুমাত্র এম (পুরুষ), এফ (মহিলা) বা, ও (অন্যান্য) অনুমতি দেয়।

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
   public static void main( String args[] ) {
      //Regular expression to match M or, F or, O
      String regex = "M|F|O";
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter students gender:");
      String name = sc.nextLine();
      Pattern p = Pattern.compile(regex);
      Matcher m = p.matcher(name);
      if(m.matches()) {
         System.out.println("All OK");
      } else {
         System.out.println("Wrong Input");
      }
   }
}

আউটপুট 1

Enter students gender:
M
All OK

আউটপুট 2

Enter students gender:
male
Wrong Input

  1. জাভাতে রেগুলার এক্সপ্রেশন রি* মেটাক্যারেক্টার

  2. নিয়মিত অভিব্যক্তি। (ডট) জাভাতে মেটাক্যারেক্টার

  3. জাভাতে রেগুলার এক্সপ্রেশন $ (ডলার) মেটাক্যারেক্টার

  4. জাভাতে রেগুলার এক্সপ্রেশন ^ (ক্যারেট) মেটাক্যারেক্টার