English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Enable multiline mode.
By default, the meta characters ^ and $ match the beginning and end of the specified characters, regardless of the number of lines.
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 w"3codebox\n" Pattern pattern = Pattern.compile("^([-9]+.*");//, Pattern.MULTILINE); Matcher matcher = pattern.matcher(input); System.out.println(matcher.group(1)); } } }
Output Result
2234
When this value is used ascompile()
When the flag value of the method is used, the entire input sequence is considered a single line, and the meta characters ^ and $ match the beginning and end of the given input sequence.
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 w"3codebox\n" Pattern pattern = Pattern.compile("^([-9]+.*", Pattern.MULTILINE); Matcher matcher = pattern.matcher(input); System.out.println(matcher.group(1)); } } }
Output Result
2234 1424