Field DOTALL ของคลาส Pattern เปิดใช้งาน dotall mode โดยค่าเริ่มต้น meta character "." ใน regular expressions จับคู่ตัวอักษรทั้งหมดยกเว้น line terminators
ตัวอย่างที่ 1: ไม่ใช้ DOTALL (default)
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("Number of new line characters: \n" + count);
}
}
Output
this is a sample this is second line Number of new line characters: 36
ใน dot all mode มันจับคู่ตัวอักษรทั้งหมดรวมถึง line terminators
กล่าวอีกนัยหนึ่ง เมื่อคุณใช้เป็น flag value ในเมธอด compile() meta character "." จะจับคู่ตัวอักษรทั้งหมดรวมถึง line terminators
ตัวอย่างที่ 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("Number of new line characters: \n" + count);
}
}
Output
this is a sample this is second line Number of new line characters: 37