ฟิลด์ DOTALL ของคลาส Pattern เปิดใช้งานโหมด dotall โดยค่าเริ่มต้น “.” อักขระ Meta ในนิพจน์ทั่วไปจะจับคู่อักขระทั้งหมดยกเว้นตัวปิดบรรทัด
ตัวอย่างที่ 1
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);
}
} ผลลัพธ์
this is a sample this is second line Number of new line characters: 36
ในโหมด dot all จะจับคู่อักขระทั้งหมดรวมทั้งตัวปิดบรรทัด
กล่าวอีกนัยหนึ่งเมื่อคุณใช้ค่านี้เป็นค่าแฟล็กของเมธอดคอมไพล์ () "" อักขระ Meta จะจับคู่อักขระทั้งหมดรวมทั้งตัวปิดบรรทัด
ตัวอย่างที่ 2
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);
}
} ผลลัพธ์
this is a sample this is second line Number of new line characters: 37