java正则表达式笔记
2020-06-30 本文已影响0人
修行者12138
.
并不可以匹配任意字符,只可以匹配除"\r"、"\n"之外的任意单个字符,如果要匹配任意单个字符,可以用[\s\S]
String str = "hello\nworld";
System.out.println(str);
System.out.println(str.matches(".*"));
System.out.println(str.matches("[\\s\\S]*"));
输出
hello
world
false
true
matches方法是全部匹配,而不是局部匹配,例如,"\w"只能匹配一个字符,"\w+"才能匹配字符串
System.out.println("abc".matches("\\w"));
System.out.println("abc".matches("\\w+"));
输出
false
true