সাবএক্সপ্রেশন/মেটাক্যারেক্টার "$" লাইনের শেষ ম্যাচ করে।
উদাহরণ ১: "Tutorialspoint" দিয়ে শেষ
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EndWith {
public static void main(String args[]) {
String regex = "Tutorialspoint$";
String input = "Hi how are you welcome to Tutorialspoint";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
int count = 0;
while (m.find()) {
count++;
System.out.println("ম্যাচের সংখ্যা: " + count);
}
}
}
আউটপুট
ম্যাচের সংখ্যা: 1
উদাহরণ ২: ডিজিট দিয়ে শেষ হওয়া স্ট্রিং খুঁজা
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EndingwithDigit {
public static void main(String args[]) {
String regex = "^.*[0-9]$";
Scanner sc = new Scanner(System.in);
System.out.println("৫টি ইনপুট স্ট্রিং দিন: ");
String input[] = new String[5];
for (int i = 0; i < 5; i++) {
input[i] = sc.nextLine();
}
Pattern p = Pattern.compile(regex);
System.out.println("ডিজিট দিয়ে শেষ হওয়া স্ট্রিংগুলো: ");
for (int i = 0; i < 5; i++) {
Matcher m = p.matcher(input[i]);
if (m.matches()) {
System.out.println(m.group());
}
}
}
}
আউটপুট
৫টি ইনপুট স্ট্রিং দিন: sample string 1 sample string 2 test data hello 3264533 3546373 7653 ডিজিট দিয়ে শেষ হওয়া স্ট্রিংগুলো: sample string 1 sample string 2 3264533 3546373 7653
নোট: $ ডিফল্টে স্ট্রিংয়ের শেষ। Pattern.MULTILINE ফ্ল্যাগ থাকলে প্রতিটি লাইনের শেষে ম্যাচ করে। \\z এবং \\Z স্ট্রিংয়ের আসলো৫ এন্ড।