여러 줄 모드를 활성화합니다.
일반적으로 ^ 및 $ 메타 문자는 줄 수에 관계없이 지정된 문자로 주어진 입력의 시작과 끝을 일치시킵니다.
예시 1
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MULTILINE_Example {
public static void main( String args[] ) {
//String regex = "(^This)";//.*t$)";
String input = "2234 This is a sample text\n"
+ "1424 This second 2335 line\n"
+ "This id third 455 line\n"
+ "Welcome to Tutorialspoint\n";
Pattern pattern = Pattern.compile("^([0-9]+).*");//, Pattern.MULTILINE);
Matcher matcher = pattern.matcher(input);
while(matcher.find()) {
System.out.println(matcher.group(1));
}
}
} 출력
2234
이것을 compile() 메서드에 대한 플래그 값으로 사용하면 전체 입력 시퀀스가 한 줄로 처리되고 메타 문자 ^ 및 $가 지정된 입력 시퀀스의 시작과 끝과 일치합니다.
예시 2
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MULTILINE_Example {
public static void main( String args[] ) {
//String regex = "(^This)";//.*t$)";
String input = "2234 This is a sample text\n"
+ "1424 This second 2335 line\n"
+ "This id third 455 line\n"
+ "Welcome to Tutorialspoint\n";
Pattern pattern = Pattern.compile("^([0-9]+).*", Pattern.MULTILINE);
Matcher matcher = pattern.matcher(input);
while(matcher.find()) {
System.out.println(matcher.group(1));
}
}
} 출력
2234 1424