কম্পিউটার

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


সাব এক্সপ্রেশন/মেটাচ্যারেক্টার “\b ” বন্ধনীর বাইরে থাকলে শব্দের সীমানার সাথে মেলে। বন্ধনীর ভিতরে থাকাকালীন ব্যাকস্পেস (0x08) এর সাথে মেলে।

উদাহরণ 1

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 = "\\bbecause\\b";
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a string: ");
      String input = sc.nextLine();
      Pattern p = Pattern.compile(regex);
      Matcher m = p.matcher(input);
      int count = 0;
      while(m.find()) {
         count ++;
      }
      System.out.println("Number of matches: "+count);
   }
}

আউটপুট

Enter a string:
A sentence doesn't end with because because, because is a conjunction
Number of matches: 3

উদাহরণ 2

নিম্নলিখিত জাভা উদাহরণ ব্যবহারকারীর কাছ থেকে একটি স্ট্রিং মান পড়ে এবং শব্দের সীমানা সংখ্যা প্রিন্ট করে।

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
   public static void main( String args[] ) {
      System.out.println("Enter input string: ");
      Scanner sc = new Scanner(System.in);
      String input = sc.nextLine();
      String regex = "\\b";
      //Compiling the regular expression
      Pattern pattern = Pattern.compile(regex);
      //Retrieving the matcher object
      Matcher matcher = pattern.matcher(input);
      int count =0;
      while(matcher.find()) {
         count ++;
      }
      System.out.println(count);
   }
}

আউটপুট

Enter input string:
Hello how are you welcome to Tutorialspoint
14

  1. জাভাতে রেগুলার এক্সপ্রেশন \A মেটাক্যারেক্টার ব্যাখ্যা কর

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

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

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