কম্পিউটার

কিভাবে জাভা RegEx ব্যবহার করে একটি অভিব্যক্তির সংঘটনের n সংখ্যা মেলে?


জাভা দ্বারা প্রদত্ত লোভী কোয়ান্টিফায়ারগুলি আপনাকে একটি অভিব্যক্তির একাধিক ঘটনার সাথে মেলাতে অনুমতি দেয়। কোথায়,

  • Exp{n} ঠিক n বার এক্সপ্রেশন এক্সপ্রেশনের সংঘটনকে প্ররোচিত করে।

  • Exp{n,} অন্তত n বার এক্সপ্রেশনের সংঘটনকে প্ররোচিত করে।

  • Exp{n, m} এক্সপ্রেশন এক্সপ্রেশনের সংঘটনকে অনুপ্রাণিত করে অন্তত n এবং সর্বোচ্চ m বার।

উদাহরণ 1

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 accept 5 letter word
      String regex = "\\w{5}";
      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:
rama
raja
raghu
megha
malya
rama: not accepted
raja: not accepted
raghu: accepted
megha: accepted
malya: accepted

উদাহরণ 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 a string of non-word with length 2 to 6
      String regex = "\\W{2,6}";
      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]+" matched");
         }
      }
   }
}

আউটপুট 1

Enter 5 input strings:
hello how are you
#$#%
#
#$@%%#&#&
sample text
#$#% matched
#$@%%#&#& matched

উদাহরণ 3

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 = "[a-zA-Z]{1,20}";
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter students name:");
      String name = sc.nextLine();
      Pattern p = Pattern.compile(regex);
      Matcher m = p.matcher(name);
      if(m.matches()) {
         System.out.println("Name is appropriate");
      } else {
         System.out.println("Name is inappropriate");
      }
   }
}

আউটপুট

Enter students name:
Mouktika
Name is appropriate

  1. Java RegEx ব্যবহার করে একটি নির্দিষ্ট স্ট্রিং/লাইনের শেষের সাথে কীভাবে মিলবে

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

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

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