正则表达式:指一个用来描述或者匹配一系列符合某个字符串的单个字符串;作用:注册邮箱,用户名,密码,对其长度或者格式进行限制
尽管正常的语句也可以限制但是太过麻烦,通常代码限制需要十几行甚至100行,而用正则表达式只需要一行,这就体现了正则表达式的优点。下面来具体介绍一下正则表达式:
字符匹配:匹配单个字符
1.a:表示匹配字符a
2.\\:表示匹配转义字符"\"
3.\t:表示匹配转义字符"\t"
4.\n:表示匹配转义字符"\n"
一组字符:任意匹配里面的一个字符
1.[abc]:表示abc里面的任何一个
2.[^abc:]:除了abc之外的任意一个(^表示取反)
3.[a-zA-Z]:a-z和A-Z的任意一个(包括边界)
4.[a-d[m-p]]:等同于[a-dm-p]相当于并集,也就是a-d和m-p的任意一个
5.[a-z&&[def]]:既是def内的任意一个还是a-z的任意一个,相当于交集,等于[def]
6.[a-z&&[^abc]]:除了abc以外的a-z的任意一个
简写表达式:某些条件的简便写法
1..:表示任意字符
2.\\d表示任意数字,相当于[0-9]
3.\\D表示任意非数字,相当于[^0-9]
4.\\w表示单个字符,相当于[0-9A-Za-z_]
5\\W代表.[^0-9A-Za-z_]
6.\\s代表空字符如:\t,\r,\b0x,\n
7.\\S代表非空字符
数量字符:
1.[]?:代表中括号内的任意单个字符出现一次或者一次都不出现
2.[]*:代表中括号内的任意单个字符不出现,或者出现无穷次
3.[]+:代表中括号内的任意单个字符出现一次或者出现多次
4.[]{n}:代表中括号内的任意单个字符恰好出现n次
5.[]{n,}:代表中括号内的任意单个字符至少出现n次
6.[]{m,n}:代表中括号内的任意单个字符至少出现n次,最多出现m次
边界匹配
^:在javascript中代表一组正则的开始
$:在javascript中代表一组正则的结束
例子:
package Regex; public class Regex { public static void main(String[] args) {//[]内表示的是一个字符 demo1(); demo2(); demo3(); demo4(); demo5(); demo6(); demo7(); demo8(); demo9(); demo10(); demo11(); demo12(); demo13(); demo14(); demo15(); demo16(); demo17(); demo18(); demo19(); } public static void demo19() { String regex ="[abc]{4,6}";//出现四次到六次 System.out.println("以下是对[abc]{4,6}的测试结果"); System.out.println("abcca "+"abcca".matches(regex)); System.out.println("bbc "+"bbc".matches(regex)); System.out.println("aaaabbbbcccc "+"aaaabbbbcccc".matches(regex)); } public static void demo18() { String regex ="[abc]{4,}";//至少出现四次 System.out.println("以下是对[abc]{4,}的测试结果"); System.out.println("abcca "+"abcca".matches(regex)); System.out.println("bbc "+"bbc".matches(regex)); System.out.println("aaaabbbbbcccc "+"aaaabbbbcccc".matches(regex)); } public static void demo17() { String regex ="[abc]{4}";//恰好出现4次 System.out.println("以下是对[abc]{4}的测试结果"); System.out.println("abc "+"abc".matches(regex)); System.out.println("abcc "+"abcc".matches(regex)); System.out.println("aaaabbbbcccc "+"aaaabbbbcccc".matches(regex)); } public static void demo16() { String regex="[abc]+";//表示出现一次或者多次 System.out.println("以下是对[abc]+的测试结果"); System.out.println("abc "+"abc".matches(regex)); System.out.println("bbcc "+"bbcc".matches(regex)); System.out.println("aabbd "+"aabbd".matches(regex)); } public static void demo15() { String regex="[abc]*";//*表示出现零次或者多次 System.out.println("以下是对[abc]*的测试结果"); System.out.println("abc "+"abc".matches(regex)); System.out.println("aabbcc "+"aabbcc".matches(regex)); System.out.println("aabd "+"aabd".matches(regex)); } public static void demo14() { String regex ="[abc]?";//?表示出现一次或者一次都没有出现 System.out.println("以下是对[abc]?的测试结果"); System.out.println("a "+"a".matches(regex)); System.out.println("abc "+"abc".matches(regex)); System.out.println("aa "+"aa".matches(regex)); System.out.println("aab "+"aab".matches(regex)); } public static void demo13() { String regex ="[\\S]";//表示任意的空白字符 System.out.println("以下是对[\\S]的测试结果"); System.out.println("a "+"a".matches(regex)); System.out.println("Z"+"Z".matches(regex)); System.out.println("回车 "+"\\r".matches(regex)); System.out.println("_ "+"_".matches(regex)); System.out.println("空格 "+" ".matches(regex)); } public static void demo12() { String regex="[\\s]";//表示任意的空白字符如\x0b(垂直制表符),\r(回车),\t(制表符),\f(翻页),\n(换行) System.out.println("以下是对[\\s]的测试结果"); System.out.println("a "+"a".matches(regex)); System.out.println("Z"+"Z".matches(regex)); System.out.println("回车\r"+"\r".matches(regex)); System.out.println("_ "+"_".matches(regex)); System.out.println("空格 "+" ".matches(regex)); System.out.println("tab "+" ".matches(regex)); } public static void demo11() { String regex="[\\W]";//表示非单词字母,等价于[^a-zA-Z_0-9] System.out.println("以下是对[\\W]的测试结果"); System.out.println("a "+"a".matches(regex)); System.out.println("Z "+"Z".matches(regex)); System.out.println("9 "+"9".matches(regex)); System.out.println("_ "+"_".matches(regex)); System.out.println("空格"+" ".matches(regex)); } public static void demo10() { String regex="[\\w]";//表示单词字符与[a-zA-Z_0-9]相等 System.out.println("以下是对[\\w]的测试结果"); System.out.println("a "+"a".matches(regex)); System.out.println("Z "+"Z".matches(regex)); System.out.println("9 "+"9".matches(regex)); System.out.println("_ "+"_".matches(regex)); } public static void demo9() { String regex ="[\\D]";//大写的D表示非数字与[^0-9]表示相同的意思 System.out.println("以下是对[\\D]的测试结果"); System.out.println("a "+"a".matches(regex)); System.out.println("z "+"z".matches(regex)); System.out.println("9 "+"9".matches(regex)); } public static void demo8() { String regex =".";//.表示任意单个字符 System.out.println("以下是对.的测试结果"); System.out.println("a "+"a".matches(regex)); System.out.println("z "+"z".matches(regex)); System.out.println("9 "+"9".matches(regex)); System.out.println("tab "+" ".matches(regex));//一个tab键 System.out.println("空格 "+" ".matches(regex));//一个空格 System.out.println("四个空格"+" ".matches(regex));//四个空格,虽然与tab键的长度相等但是,tab一个字符,一个空格一个字符。 } public static void demo7() { String regex ="[\\d]";//在\d的时候会报错,因为转义,所以还需要一个\才能代表数字,相当于[0-9] System.out.println("以下是对[\\d]的测试结果"); System.out.println("a "+"a".matches(regex)); System.out.println("z "+"z".matches(regex)); System.out.println("9 "+"9".matches(regex)); } public static void demo6() { String regex ="[a-z&&[^abc]]";//表示除了abc以外的所有小写字母 System.out.println("以下是对[a-z&&[^abc]]的测试结果"); System.out.println("a "+"a".matches(regex)); System.out.println("z "+"z".matches(regex)); } public static void demo5() { String regex="[a-z&&[def]]";//相当于交集,即[def] System.out.println("以下是对[a-z&&[def]]的测试结果"); System.out.println("a "+"a".matches(regex)); System.out.println("d "+"d".matches(regex)); } public static void demo4() { String regex ="[a-d[m-p]]";//[a-d[m-p]]相当于[a-dm-p] System.out.println("以下是对[a-d[m-p]]的测试结果"); System.out.println("a "+"a".matches(regex)); System.out.println("n "+"n".matches(regex)); } public static void demo3() { String regex ="[a-zA-Z]";//包含边界 System.out.println("以下是对[a-zA-Z]的测试结果"); System.out.println("a "+"a".matches(regex)); System.out.println("M "+"M".matches(regex)); System.out.println("空格 "+" ".matches(regex)); } public static void demo2() { String regex="[^abc]";//^表示取反,也就是非,除了abc都可以,但只能是一个字符 System.out.println("以下是对[^abc]的测试结果"); System.out.println("a "+"a".matches(regex)); System.out.println("9 "+"9".matches(regex)); } public static void demo1() { String regex ="[abc]"; //[abc]表示的是a或b或c且只能有一个 System.out.println("以下是对[abc]的测试结果"); System.out.println("a "+"a".matches(regex)); System.out.println("ab "+"ab".matches(regex)); } }
运行结果:
可以发现正则表达式使用起来非常的简便。
原文:https://www.cnblogs.com/tkg1314/p/11819371.html