কম্পিউটার

জাভা RegEx ব্যবহার করে দুটি প্রদত্ত এক্সপ্রেশনের একটির সাথে কিভাবে মিলবে?


বা লজিক্যাল অপারেটর ব্যবহার করে | জাভা রেগুলার এক্সপ্রেশনের আপনি দুটি প্রদত্ত এক্সপ্রেশনের যেকোনো একটির সাথে মিলতে পারেন।

উদাহরণস্বরূপ, যদি আপনার প্রয়োজন হয় যে আপনার রেগুলার এক্সপ্রেশনটি একাধিক এক্সপ্রেশনের সাথে মিলিত হোক আপনি প্রয়োজনীয় এক্সপ্রেশনগুলিকে “|” দ্বারা আলাদা করে তা করতে পারেন।

উদাহরণ 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

  1. কিভাবে জাভা রেজেক্স ব্যবহার করে অক্ষরের একটি পরিসীমা মেলে

  2. জাভা RegEx ব্যবহার করে অক্ষরের একটি নির্দিষ্ট সেটের সাথে কীভাবে মিলানো যায়

  3. কিভাবে Java RegEx ব্যবহার করে যেকোন অক্ষরের সাথে মেলে

  4. জাভাতে Gson ব্যবহার করে JSON এর বৈশিষ্ট্যগুলি কীভাবে পুনঃনামকরণ করবেন?