用示例对Java中的UNIX_LINES字段进行模式化
该标志启用Unix行模式。在Unix行模式下,仅将'\n'用作行终止符,而将'\r'视为文字字符。
例子1
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LTERAL_Example {
public static void main(String[] args) {
String input = "This is the first line\r"
+ "This is the second line\r"
+ "This is the third line\r";
//正则表达式以MM-DD-YYY格式接受日期
String regex = "^T.*e";
//创建一个Pattern对象
Pattern pattern = Pattern.compile(regex, Pattern.UNIX_LINES);
//创建一个Matcher对象
Matcher matcher = pattern.matcher(input);
int count = 0;
while(matcher.find()) {
count++;
System.out.println(matcher.group());
}
System.out.println("Number of matches: "+count);
}
}输出结果
This is the first line This is the second line This is the third line Number of matches: 1
而在正常模式下,\r被视为回车。
例子2
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LTERAL_Example {
public static void main(String[] args) {
String input = "This is the first line\r"
+ "This is the second line\r"
+ "This is the third line\r";
//正则表达式以MM-DD-YYY格式接受日期
String regex = "^T.*e";
//创建一个Pattern对象
Pattern pattern = Pattern.compile(regex);
//创建一个Matcher对象
Matcher matcher = pattern.matcher(input);
int count = 0;
while(matcher.find()) {
count++;
System.out.println(matcher.group());
}
System.out.println("Number of matches: "+count);
}
}输出结果
This is the first line Number of matches: 1
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短