মাল্টিলাইন মোড (multiline mode) সক্ষম করে।
সাধারণত, ^ এবং $ মেটা ক্যারেক্টারগুলো দেওয়া ইনপুটের শুরু এবং শেষ নির্দিষ্ট ক্যারেক্টারসহ মিলায়, এর লাইনের সংখ্যা কত সেটা विचায় না।
উদাহরণ ১
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MULTILINE_Example {
public static void main(String args[]) {
// String regex = "(^This)";//.*t$)
String input = "2234 This is a sample text\n"
+ "1424 This second 2335 line\n"
+ "This id third 455 line\n"
+ "Welcome to Tutorialspoint\n";
Pattern pattern = Pattern.compile("^([0-9]+).*");//, Pattern.MULTILINE);
Matcher matcher = pattern.matcher(input);
while(matcher.find()) {
System.out.println(matcher.group(1));
}
}
}
আউটপুট
2234
যখন আপনি compile() মেথডে এই ফ্ল্যাগ মান হিসেবে ব্যবহার করেন, পুরো ইনপুট সিকোয়েন্সটি একটি একক লাইন হিসেবে বিবেচিত হয় এবং মেটা ক্যারেক্টার ^ এবং $ দেওয়া ইনপুট সিকোয়েন্সের শুরু এবং শেষ মিলায়।
উদাহরণ ২
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MULTILINE_Example {
public static void main(String args[]) {
// String regex = "(^This)";//.*t$)
String input = "2234 This is a sample text\n"
+ "1424 This second 2335 line\n"
+ "This id third 455 line\n"
+ "Welcome to Tutorialspoint\n";
Pattern pattern = Pattern.compile("^([0-9]+).*", Pattern.MULTILINE);
Matcher matcher = pattern.matcher(input);
while(matcher.find()) {
System.out.println(matcher.group(1));
}
}
}
আউটপুট
2234 1424