标签:
package date0804.demo1; import java.util.regex.Pattern; public class RegularExpres { public static void main(String[] args) { //中括号只代表一位字符,*代表任意位,问号代表一位 //匹配首字母为a,末尾字母为b的任意字符串 boolean b1 = Pattern.matches("a*d", "aaaaad"); //匹配首字母为a,末尾字母为b的中间为任意一个字符(包括空字符)的任意字符串 boolean b2 = Pattern.matches("a?d", "ad"); //匹配首字母为a,末尾字母为b,中间为a或b或c的任意字符串 boolean b3 = Pattern.matches("a[abc]d", "abd"); //匹配首字母为a,末尾字母为b,中间为排除a或b或c的任意字符串 boolean b4 = Pattern.matches("a[^abc]d", "abd"); //匹配a-z或A-Z任意字符 boolean b5 = Pattern.matches("[a-zA-Z]", "a"); //a-d,m-p boolean b6 = Pattern.matches("[a-dm-p]", "l"); //d,e,f boolean b7 = Pattern.matches("[a-z&&[def]]", "d"); //a-d,m-p boolean b8 = Pattern.matches("[a-dm-p]", "l"); //digit boolean b9 = Pattern.matches("[0-9]", "?"); } }
标签:
原文地址:http://my.oschina.net/zzw922cn/blog/487436