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