English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
다중 행 모드를 활성화합니다.
일반적으로, ^와 $ 메타 캐릭터는 주어진 입력의 시작과 끝을 지정된 문자와 일치시키며, 행 수와 관계없이 일치합니다.
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 w3codebox\n"; Pattern pattern = Pattern.compile("^([0-9]+).*");//, Pattern.MULTILINE); Matcher matcher = pattern.matcher(input); System.out.println(matcher.group(1)); } } }
출력 결과
2234
이 값을 사용할 때,compile()
메서드의 표시값을 사용할 때, 전체 입력 시퀀스가 단일 행으로 간주되며, 메타 캐릭터 ^와 $가 주어진 입력 시퀀스의 시작과 끝과 일치합니다.
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 w3codebox\n"; Pattern pattern = Pattern.compile("^([0-9]+).*", Pattern.MULTILINE); Matcher matcher = pattern.matcher(input); System.out.println(matcher.group(1)); } } }
출력 결과
2234 1424