java.util.regex.Matcher ক্লাস একটি ইঞ্জিনের প্রতিনিধিত্ব করে যা বিভিন্ন ম্যাচ অপারেশন করে। এই ক্লাসের জন্য কোন কনস্ট্রাক্টর নেই, আপনি java.util.regex.Pattern ক্লাসের matches() পদ্ধতি ব্যবহার করে এই ক্লাসের একটি অবজেক্ট তৈরি/প্রাপ্ত করতে পারেন।
গোষ্ঠী() এই (Matcher) ক্লাসের পদ্ধতিটি শেষ ম্যাচের সময় মিলিত ইনপুট অনুগামী প্রদান করে।
উদাহরণ 1
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GroupExample {
public static void main(String[] args) {
String str = "<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>. "
+ "It <i>also</i> contains <i>italic</i> words</p>";
//Regular expression to match contents of the bold tags
String regex = "<b>(\\S+)</b>|<i>(\\S+)</i>";
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Matching the compiled pattern in the String
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
} আউটপুট
<b>is</b> <b>example</b> <b>script</b> <b>every</b> <b>word</b> <b>bold</b> <i>also</i> <i>italic</i>
এই পদ্ধতির আরেকটি বৈকল্পিক গোষ্ঠীর প্রতিনিধিত্বকারী একটি পূর্ণসংখ্যা ভেরিয়েবল গ্রহণ করে, যেখানে ক্যাপচার করা গ্রুপগুলি 1 থেকে শুরু করে (বাম থেকে ডানে) সূচী করা হয়।
উদাহরণ 2
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.";
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Matching the compiled pattern in the String
Matcher matcher = pattern.matcher(input);
if(matcher.find()) {
System.out.println("match: "+matcher.group(0));
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));
}
}
} আউটপুট
match: This is a sample Text, 1234, with numbers in between. First group match: This is a sample Text, 123 Second group match: 4 Third group match: , with numbers in between.