কম্পিউটার

জাভা রেগ এক্স-এ গ্রুপ ক্যাপচার করার অর্থ কী?


একটি রেগুলার এক্সপ্রেশন হল অক্ষরগুলির একটি বিশেষ ক্রম যা আপনাকে প্যাটার্নে রাখা একটি বিশেষ সিনট্যাক্স ব্যবহার করে অন্য স্ট্রিং বা স্ট্রিংগুলির সেটগুলিকে মেলাতে বা খুঁজে পেতে সহায়তা করে৷

রেগুলার এক্সপ্রেশনে ক্যাপচারিং গ্রুপগুলি একাধিক অক্ষরকে একটি একক হিসাবে ব্যবহার করতে ব্যবহৃত হয় যা "()" দ্বারা উপস্থাপিত হয়। যেমন আপনি যদি একটি প্যারাথিসিসে একাধিক সাব প্যাটার্ন রাখেন তবে সেগুলিকে একটি গ্রুপ হিসাবে বিবেচনা করা হয়৷

উদাহরণস্বরূপ, প্যাটার্ন [0-9] 0 থেকে 9 সংখ্যার সাথে মেলে এবং প্যাটার্ন {5} যেকোনো অক্ষরের সাথে মেলে। আপনি যদি এই দুটিকে ([0-9]{5}) হিসাবে গোষ্ঠীবদ্ধ করেন , এটি একটি 5-সংখ্যার সংখ্যার সাথে মেলে৷

উদাহরণ

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class COMMENTES_Example {
   public static void main(String[] args) {  
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter your name: ");
      String name = sc.nextLine();
      System.out.println("Enter your Date of birth: ");
      String dob = sc.nextLine();  
      //Regular expression to accept date in MM-DD-YYY format
      String regex = "^(1[0-2]|0[1-9])/   # For Month\n"
       + "(3[01]|[12][0-9]|0[1-9])/ # For Date\n"
       + "[0-9]{4}$                  # For Year";
      //Creating a Pattern object
      Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS);
      //Creating a Matcher object
      Matcher matcher = pattern.matcher(dob);
      boolean result = matcher.matches();
      if(result) {
         System.out.println("Given date of birth is valid");
      }else {
         System.out.println("Given date of birth is not valid");
      }  
   }
}

আউটপুট

Enter your name:
Krishna
Enter your Date of birth:
09/26/1989
Given date of birth is valid

উদাহরণ

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GroupTest {
   public static void main(String[] args) {
      String regex = "(.*)(\\d+)(.*)";
      String input = "This is a sample Text, 1234, with numbers in between.";
      //Creating a pattern object
      Pattern pattern = Pattern.compile(regex);
      //Matching the compiled pattern in the String
      Matcher matcher = pattern.matcher(input);
      if(matcher.find()) {
         System.out.println("match: "+matcher.group(0));  
         System.out.println("First group match: "+matcher.group(1));  
         System.out.println("Second group match: "+matcher.group(2));  
         System.out.println("Third group match: "+matcher.group(3));
      }
   }
}

আউটপুট

match: This is a sample Text, 1234, with numbers in between.
First group match: This is a sample Text, 123
Second group match: 4
Third group match: , with numbers in between.

  1. জাভাতে StringIndexOutOfBoundsException কি?

  2. জাভাতে ArrayIndexOutOfBoundsException কি?

  3. জাভাতে ডাবল-বাফারিং কি?

  4. জাভা প্রোগ্রামিং কি?