正则表达式记录一下,持续更新中
public class MyRegex {
public static void main(String[] args){
// .代表一个字符
// 后面3个正则都和前一个式子或字符有关系
// * 0/1/多个前面的字符;+ 1个或多个;? 1个或0个
isMatch("a.c","abc"); // true 中间任意字符
isMatch("a.c","abca");// false 只能匹配任意一个中间字符
isMatch("aa*c","ac");// true 匹配0个a
isMatch("aa*c","aac");// true 匹配1个a
isMatch("aa*c","aaac");// true 匹配多个a
isMatch("a+c","c"); // false 不能匹配0个字符
isMatch("a+c","aaaaac");//true 1个或多个
isMatch("a?c","c"); // true 匹配0个
isMatch("a?c","ac"); // true 匹配1个
isMatch("a?c","aac"); // false 不能匹配多个
// 中括号表达式:[ ] 匹配括号内的单个字符 ; [ - ]:到 ; \- 进行转义 ; [^ ]匹配数字的以外的单个字符
isMatch("aa[abc]","aaa"); // true
isMatch("aa[abc]","aa"); // false
isMatch("aa[abc]","aabc");// false
isMatch("aa[1-8]","aa3"); // true 和下面对比看来只认识字符
isMatch("aa[1-12]","aa3");// false 只认识 1-1/2 啊
isMatch("aa[a-z]","aag"); // true
isMatch("aa[^1-4]","aa7");// true 匹配1-4以外的字符
// ^开始 $结束
isMatch("^[0-8]+abc$","65abc"); // true 分析一下:匹配首字母为数字,>=1个数字,以abc结尾
// {n} 匹配的次数 {n,m} 次
isMatch("a{3}c","aaac"); // true 匹配a3次
isMatch("a{3}c","aac"); // false a只有2次
isMatch("a{1,5}","aaaa"); // true 1-5次 这里是4次
// (),就是放在一起,用来改变运算的
isMatch("(a[a-z])+","ab");// true
isMatch("\\d","2"); // true \\d 转义匹配单个数字,明确这是字符,只能匹配单个数字
isMatch("\\d","23"); // false
isMatch(".aa\\b","aaa"); // true 以aa结尾
isMatch("[1-9][1-9]{0,1}$","98"); //true 匹配0-99的
// 匹配手机 1{3/4/5/6/7/8}{9为数字}
isMatch("^1[3-8]\\d{9}$","13155555555"); // true
// 匹配qq邮箱 >=1位数字@qq.com
isMatch("^[0-9]+@qq\\.com$","[email protected]"); //true
// ?! 预测该位置不是xxx字符结尾
isMatch("^(?!\\d+$)(?![a-zA-z]+$)[a-z0-9A-Z]{6,16}$","1235f1"); // true
} public static void isMatch(String regex,String str ){ Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); System.out.println(matcher.matches()); }}