java.util.regex.Matcher ক্লাস বিভিন্ন ম্যাচ অপারেশন সম্পাদন করার একটি ইঞ্জিন প্রতিনিধিত্ব করে। এই ক্লাসের কোনো কনস্ট্রাক্টর নেই, আপনি java.util.regex.Pattern ক্লাসের matches() মেথড ব্যবহার করে এই ক্লাসের অবজেক্ট তৈরি/প্রाप्त করতে পারেন।
groupCount() মেথড (Matcher ক্লাসের) বর্তমান ম্যাচে কতটা ক্যাপচারিং গ্রুপ (capturing groups) আছে তা গণনা করে।
উদাহরণ ১
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.";
// Pattern অবজেক্ট তৈরি
Pattern pattern = Pattern.compile(regex);
// স্ট্রিং-এ কম্পাইল করা প্যাটার্ন মিলানো
Matcher matcher = pattern.matcher(input);
if(matcher.find()) {
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));
System.out.println("Number of groups capturing: " + matcher.groupCount());
}
}
}
আউটপুট
First group match: This is a sample Text, 123 Second group match: 4 Third group match: , with numbers in between. Number of groups: 3
উদাহরণ ২
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String str1 = "<p>This <b>is</b> an <b>example</b>HTML <b>script</b> where <b>every</b> alternative <b>word</b> is <b>bold</b></p>.";
// বোল্ড ট্যাগের কন্টেন্ট মিলাতে রেগুলার এক্সপ্রেশন
String regex = "(t(\\S+)t)(\\s)";
String str = "the words tit tat tweet tostff tact that tilt text start and end wit the letter t ";
// Pattern অবজেক্ট তৈরি
Pattern pattern = Pattern.compile(regex);
// স্ট্রিং-এ কম্পাইল করা প্যাটার্ন মিলানো
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(0));
}
System.out.println("Total capturing groups: " + matcher.groupCount());
}
}
আউটপুট
tit tat tweet tact that tilt text tart Total capturing groups: 3