सबएक्सप्रेशन/मेटाकैरेक्टर “\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("एक स्ट्रिंग दर्ज करें: ");
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
उदाहरण 2
निम्नलिखित Java उदाहरण उपयोगकर्ता से एक स्ट्रिंग मान पढ़ता है और वर्ड बाउंड्री की संख्या प्रिंट करता है।
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 matcher = pattern.matcher(input);
int count =0;
while(matcher.find()) {
count ++;
}
System.out.println(count);
}
}
आउटपुट
इनपुट स्ट्रिंग दर्ज करें: Hello how are you welcome to Tutorialspoint 14