Java中的模式DOTALL字段的示例
Pattern类的DOTALL字段启用dotall模式。默认情况下,“。”正则表达式中的元字符与除行终止符外的所有字符匹配。
例子1
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DOTALL_Example {
public static void main( String args[] ) {
String regex = ".";
String input = "this is a sample \nthis is second line";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
int count =0;
while(matcher.find()) {
count++;
System.out.print(matcher.group());
}
System.out.println();
System.out.println("Number of new line characters: \n"+count);
}
}输出结果
this is a sample this is second line Number of new line characters: 36
在点所有模式下,它与所有字符匹配,包括行终止符。
换句话说,当您将其用作compile()方法的标志值时,“。”元字符匹配所有字符,包括行终止符。
例子2
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DOTALL_Example {
public static void main( String args[] ) {
String regex = ".";
String input = "this is a sample \nthis is second line";
Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
Matcher matcher = pattern.matcher(input);
int count = 0;
while(matcher.find()) {
count++;
System.out.print(matcher.group());
}
System.out.println();
System.out.println("Number of new line characters: \n"+count);
}
}输出结果
this is a sample this is second line Number of new line characters: 37
热门推荐
10 护士辞职送行祝福语简短
11 大寿爸爸祝福语简短英文
12 比较简短浪漫的祝福语
13 启程出发路途祝福语简短
14 打牌翻盘祝福语简短
15 上岸离职祝福语大全简短
16 白日庆典祝福语简短英语
17 祖国七十华诞简短祝福语
18 文学短句祝福语大全简短