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 生日开业蛋糕祝福语简短