কম্পিউটার

Java regex ব্যবহার করে কেস নির্বিশেষে কিভাবে একটি স্ট্রিং মেলে।


প্যাটার ক্লাসের কম্পাইল পদ্ধতি দুটি প্যারামিটার গ্রহণ করে −

  • রেগুলার এক্সপ্রেশনের প্রতিনিধিত্বকারী একটি স্ট্রিং মান।
  • একটি পূর্ণসংখ্যার মান প্যাটার্ন শ্রেণীর একটি ক্ষেত্র।

প্যাটার্ন ক্লাসের এই CASE_INSENSITIVE ক্ষেত্রটি কেস নির্বিশেষে অক্ষরের সাথে মেলে। তাই, যদি আপনি আপনার রেগুলার এক্সপ্রেশন সহ compile() পদ্ধতিতে ফ্ল্যাগ মান হিসেবে পাস করেন, তাহলে উভয় ক্ষেত্রেই অক্ষর মিলে যাবে।

উদাহরণ 1

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
   public static void main( String args[] ) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter input data: ");
      String input = sc.nextLine();
      //Regular expression to find the required character
      String regex = "test";
      //Compiling the regular expression
      Pattern pattern = Pattern.compile(regex);//, Pattern.CASE_INSENSITIVE);
      //Retrieving the matcher object
      Matcher matcher = pattern.matcher(input);
      int count =0;
      while (matcher.find()) {
         count++;
      }
      System.out.println("Number of occurrences: "+count);
   }
}

আউটপুট

Enter input data:
test TEST Test sample data
Number of occurrences: 3

উদাহরণ 2

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class VerifyBoolean {
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a string value: ");
      String str = sc.next();
      Pattern pattern = Pattern.compile("true|false", Pattern.CASE_INSENSITIVE);
      Matcher matcher = pattern.matcher(str);
      if(matcher.matches()){
         System.out.println("Given string is a boolean type");
      } else {
         System.out.println("Given string is not a boolean type");
      }
   }
}

আউটপুট

Enter a string value:
TRUE
Given string is a boolean type

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

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

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

  4. জাভাতে রেজেক্স ব্যবহার করে কীভাবে একটি স্ট্রিং থেকে একটি এইচটিএমএল ট্যাগ বের করবেন?