প্যাটার্ন java.util.regex প্যাকেজের ক্লাস হল একটি রেগুলার এক্সপ্রেশনের একটি সংকলিত উপস্থাপনা৷
বিভক্ত() এই শ্রেণীর পদ্ধতি একটি CharSequence গ্রহণ করে অবজেক্ট, ইনপুট স্ট্রিংকে প্যারামিটার হিসেবে উপস্থাপন করে এবং প্রতিটি ম্যাচে প্রদত্ত স্ট্রিংকে একটি নতুন টোকেনে বিভক্ত করে এবং সমস্ত টোকেন ধারণ করে স্ট্রিং অ্যারে ফেরত দেয়।
উদাহরণ
import java.util.regex.Pattern;
public class SplitMethodExample {
public static void main( String args[] ) {
//Regular expression to find digits
String regex = "(\\s)(\\d)(\\s)";
String input = " 1 Name:Radha, age:25 2 Name:Ramu, age:32 3 Name:Rajev, age:45";
//Compiling the regular expression
Pattern pattern = Pattern.compile(regex);
//verifying whether match occurred
if(pattern.matcher(input).find())
System.out.println("Given String contains digits");
else
System.out.println("Given String does not contain digits");
//Splitting the string
String strArray[] = pattern.split(input);
for(int i=0; i<strArray.length; i++){
System.out.println(strArray[i]);
}
}
} আউটপুট
Given String contains digits Name:Radha, age:25 Name:Ramu, age:32 Name:Rajev, age:45
এই পদ্ধতিটি একটি পূর্ণসংখ্যা মানও গ্রহণ করে যা প্যাটার্ন প্রয়োগের সংখ্যাকে প্রতিনিধিত্ব করে। অর্থাৎ আপনি সীমা মান উল্লেখ করে ফলাফলের অ্যারের দৈর্ঘ্য নির্ধারণ করতে পারেন।
উদাহরণ
import java.util.regex.Pattern;
public class SplitMethodExample {
public static void main( String args[] ) {
//Regular expression to find digits
String regex = "(\\s)(\\d)(\\s)";
String input = " 1 Name:Radha, age:25 2 Name:Ramu, age:32" + " 3 Name:Rajeev, age:45 4 Name:Raghu, age:35" + " 5 Name:Rahman, age:30";
//Compiling the regular expression
Pattern pattern = Pattern.compile(regex);
//verifying whether match occurred
if(pattern.matcher(input).find())
System.out.println("Given String contains digits");
else
System.out.println("Given String does not contain digits");
//Splitting the string
String strArray[] = pattern.split(input, 4);
for(int i=0; i<strArray.length; i++){
System.out.println(strArray[i]);
}
}
} আউটপুট
Given String contains digits Name:Radha, age:25 Name:Ramu, age:32 Name:Rajeev, age:45 4 Name:Raghu, age:35 5 Name:Rahman, age:30