Pattern क्लास का DOTALL फील्ड डॉटऑल मोड को सक्षम करता है। डिफ़ॉल्ट रूप से, रेगुलर एक्सप्रेशन में "." मेटा कैरेक्टर लाइन टर्मिनेटर्स को छोड़कर सभी वर्णों से मिलान करता है।
उदाहरण 1 (डिफ़ॉल्ट मोड - DOTALL के बिना)
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DOTALL_Example {
public static void main(String args[]) {
String regex = ".";
String input = "this is a sample \nthis is second line";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
int count = 0;
while(matcher.find()) {
count++;
System.out.print(matcher.group());
}
System.out.println();
System.out.println("नई लाइन वर्णों की संख्या: \n" + count);
}
}
आउटपुट
this is a sample this is second line नई लाइन वर्णों की संख्या: 36
डॉट ऑल मोड में यह लाइन टर्मिनेटर्स सहित सभी वर्णों से मिलान करता है।
दूसरे शब्दों में, जब आप इसे compile() मेथड के फ्लैग मान के रूप में उपयोग करते हैं, तो "." मेटा कैरेक्टर लाइन टर्मिनेटर्स सहित सभी वर्णों से मिलान करता है।
उदाहरण 2 (DOTALL फ्लैग के साथ)
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DOTALL_Example {
public static void main(String args[]) {
String regex = ".";
String input = "this is a sample \nthis is second line";
Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
Matcher matcher = pattern.matcher(input);
int count = 0;
while(matcher.find()) {
count++;
System.out.print(matcher.group());
}
System.out.println();
System.out.println("नई लाइन वर्णों की संख्या: \n" + count);
}
}
आउटपुट
this is a sample this is second line नई लाइन वर्णों की संख्या: 37