জাভা রেগুলার এক্সপ্রেশন-এ \b মেটা ক্যারেক্টার শব্দের সীমানা (word boundaries) মিলাতে ব্যবহৃত হয়। তাই, দেওয়া ইনপুট টেক্সট থেকে কোনো নির্দিষ্ট শব্দ খুঁজতে হলে রেগুলার এক্সপ্রেশনে সেই শব্দটি শব্দ সীমানার ভিতরে রাখতে হয়:
"\\brequired word\\b";
উদাহরণ ১
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MachingWordExample1 {
public static void main(String args[]) {
// স্ট্রিং মান পড়া
Scanner sc = new Scanner(System.in);
System.out.println("Enter input string");
String input = sc.next();
// অঙ্ক খুঁজতে রেগুলার এক্সপ্রেশন
String regex = "\\bhello\\b";
// রেগুলার এক্সপ্রেশন কম্পাইল
Pattern pattern = Pattern.compile(regex);
// Matcher অবজেক্ট পাওয়া
Matcher matcher = pattern.matcher(input);
if(matcher.find()) {
System.out.println("Match found");
} else {
System.out.println("Match not found");
}
}
}
আউটপুট
Enter input string hello welcome to Tutorialspoint Match found
উদাহরণ ২
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatcherExample2 {
public static void main(String args[]) {
String input = "This is sample text \n"
+ "This is second line "
+ "This is third line";
String regex = "\\bsecond\\b";
// রেগুলার এক্সপ্রেশন কম্পাইল
Pattern pattern = Pattern.compile(regex);
// Matcher অবজেক্ট পাওয়া
Matcher matcher = pattern.matcher(input);
if(matcher.find()) {
System.out.println("Match found");
} else {
System.out.println("Match not found");
}
}
}
আউটপুট
Match found