সাবএক্সপ্রেশন/মেটাক্যারেক্টার "\Z" পুরো স্ট্রিংয়ের এন্ড ম্যাচ করে, তবে ফাইনাল লাইন টার্মিনেটর (নিউলাইন) অনুমতি দেয়।
উদাহরণ ১: "Tutorialspoint" দিয়ে শেষ
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
public static void main(String args[]) {
String regex = "Tutorialspoint\\z";
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.regex.Matcher;
import java.util.regex.Pattern;
public class Data {
public static void main(String args[]) {
String regex = "[0-9]\\z";
String input = "Hi how are you \n this is sample text \n this is third line 554";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
if (m.find()) {
System.out.println("দেওয়া ইনপুট ডিজিট দিয়ে শেষ হয়েছে");
} else {
System.out.println("দেওয়া ইনপুট ডিজিট দিয়ে শেষ হয়নি");
}
}
}
আউটপুট
দেওয়া ইনপুট ডিজিট দিয়ে শেষ হয়েছে
\\z vs \\Z: \\z = স্ট্রিংয়ের абсолют এন্ড (লাইন টার্মিনেটরও ম্যাচ করে না)। \\Z = স্ট্রিংয়ের এন্ড, কিন্তু শেষে লাইন টার্মিনেটর থাকলে সেটা উপেক্ষা করে (অর্থাৎ লাইন টার্মিনেটরের ঠিক আগে ম্যাচ করে)।