Java中的模式MULTILINE字段示例
启用多行模式。
通常,^和$元字符将给定输入的开头和结尾与指定字符匹配,而不管其行数如何。
例子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 Nhooo\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 Nhooo\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
热门推荐
4 圆圆的祝福语简短
10 药店祝福语简短10字
11 启程出发路途祝福语简短
12 学生开学季祝福语简短
13 足球队祝福语简短
14 祝福语写给自己的简短
15 18岁祝福语搞笑简短
16 恭喜朋友买车祝福语简短
17 朋友孩子考试祝福语简短
18 上班收到花束祝福语简短