美文网首页
正则表达式 Java 入门一

正则表达式 Java 入门一

作者: CNSTT | 来源:发表于2018-11-26 17:05 被阅读0次

. 匹配除了换行符外任意的单字符,匹配.使用\.

String c0 = "abcdefg"; 
String p0 = ".cdefg";
boolean iM0 = Pattern.matches(p0, c0);          
System.out.println(iM0);                        //输出:false

* 匹配前面的字符串0次或n次,匹配*使用\*

String c1 = "abcdefg"; 
String p1 = ".*cde.*";
boolean iM1 = Pattern.matches(p1, c1);          
System.out.println(iM1);                        //输出:true

+ 匹配前面的字符串1次或n次,匹配+使用\+

String c2 = "abcdefg"; 
String p2 = ".+abcde.*";    
boolean iM2 = Pattern.matches(p2, c2);          
System.out.println(iM2);                        //输出:false

\\s+ 匹配任意个空格

String c3 = "this is my  friend"; 
String p3 = "this\\s+is\\s+my\\s+friend";
boolean iM3 = Pattern.matches(p3, c3);          
System.out.println(iM3);                        //输出:true

^ 定义了以什么开始

String c4 = "this is my  friend"; 
String p4 = "^this.*";
boolean iM4 = Pattern.matches(p4, c4);          
System.out.println(iM4);                        //输出:true

$ 定义了以什么结尾

String c5 = "this is my friend"; 
String p5 = ".*friend$";
boolean iM5 = Pattern.matches(p5, c5);          
System.out.println(iM5);                        //输出:true

\\d 0-9的一个数字 \\D 非数字

String c6 = "plain"; 
String p6 = "^\\D.*";   
boolean iM6 = Pattern.matches(p6, c6);          
System.out.println(iM6);                        //输出:true

相关文章

网友评论

      本文标题:正则表达式 Java 入门一

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