이 플래그는 Unix 라인 모드를 활성화합니다. Unix 라인 모드에서는 '\n'만 라인 종결자로 사용되고 '\r'은 리터럴 문자로 처리됩니다.
예시 1
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";
//Creating a Pattern object
Pattern pattern = Pattern.compile(regex, Pattern.UNIX_LINES);
//Creating a 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);
}
} 출력
This is the first line This is the second line This is the third line Number of matches: 1
반면 일반 모드에서 \r은 캐리지 리턴으로 처리됩니다.
예시 2
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";
//Creating a Pattern object
Pattern pattern = Pattern.compile(regex);
//Creating a 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);
}
} 출력
This is the first line Number of matches: 1