সাবএক্সপ্রেশন/মেটাক্যারেক্টার "\b" দুটি প্রসঙ্গে ভিন্ন অর্থ বহন করে:
- ব্র্যাকেটের বাইরে: ওয়ার্ড বাউন্ডারি ম্যাচ করে
- ব্র্যাকেটের ভিতরে [\b]: ব্যাকস্পেস (0x08) ম্যাচ করে
উদাহরণ ১: "because" শব্দের ওয়ার্ড বাউন্ডারি
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("একটি স্ট্রিং দিন: ");
String input = sc.nextLine();
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
int count = 0;
while (m.find()) {
count++;
}
System.out.println("ম্যাচের সংখ্যা: " + count);
}
}
আউটপুট
একটি স্ট্রিং দিন: A sentence doesn't end with because because, because is a conjunction ম্যাচের সংখ্যা: 3
উদাহরণ ২: স্ট্রিংয়ের ওয়ার্ড বাউন্ডারি গণনা
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("ইনপুট স্ট্রিং দিন: ");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
String regex = "\\b";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
int count = 0;
while (matcher.find()) {
count++;
}
System.out.println(count);
}
}
আউটপুট
ইনপুট স্ট্রিং দিন: Hello how are you welcome to Tutorialspoint 14
ব্যাখ্যা: "Hello how are you welcome to Tutorialspoint" -এ ৭টি শব্দ, প্রতিটি শব্দের শুরু ও শেষে বাউন্ডারি = ১৪টি।