美文网首页正则java
java正则表达式笔记

java正则表达式笔记

作者: 修行者12138 | 来源:发表于2020-06-30 22:23 被阅读0次

.并不可以匹配任意字符,只可以匹配除"\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

相关文章

网友评论

    本文标题:java正则表达式笔记

    本文链接:https://www.haomeiwen.com/subject/ncrkqktx.html