Flag นี้เปิดใช้งาน Unix lines mode ใน Unix lines mode มีเพียง '\n' เท่านั้นที่ใช้เป็น line terminator และ '\r' ถูกจัดการเป็น literal character
ตัวอย่างที่ 1: ใช้ UNIX_LINES
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LTERAL_Example {
public static void main(String[] args) {
String input = "This is the first line\r" + "This is the second line\r" + "This is the third line\r";
// Regular expression to accept date in MM-DD-YYY format
String regex = "^T.*e";
// สร้าง Pattern object
Pattern pattern = Pattern.compile(regex, Pattern.UNIX_LINES);
// สร้าง Matcher object
Matcher matcher = pattern.matcher(input);
int count = 0;
while (matcher.find()) {
count++;
System.out.println(matcher.group());
}
System.out.println("Number of matches: " + count);
}
}
Output
This is the first line This is the second line This is the third line Number of matches: 1
ในขณะที่ใน normal mode \r ถูกจัดการเป็น carriage-return
ตัวอย่างที่ 2: ไม่ใช้ UNIX_LINES (normal mode)
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LTERAL_Example {
public static void main(String[] args) {
String input = "This is the first line\r" + "This is the second line\r" + "This is the third line\r";
// Regular expression to accept date in MM-DD-YYY format
String regex = "^T.*e";
// สร้าง Pattern object
Pattern pattern = Pattern.compile(regex);
// สร้าง Matcher object
Matcher matcher = pattern.matcher(input);
int count = 0;
while (matcher.find()) {
count++;
System.out.println(matcher.group());
}
System.out.println("Number of matches: " + count);
}
}
Output
This is the first line Number of matches: 1