正则表达式简单整理

166 阅读4分钟

01正则表达式的概念和作用

* A: 正则表达式的概念和作用
	* a: 正则表达式的概述
		* 正则表达式也是一个字符串,用来定义匹配规则,在Pattern类中有简单的规则定义。
		  可以结合字符串类的方法使用。
		* 简单记:正则表达式是具有特殊含义的字符串。
	* b: 正则表达式的作用
	* 比如注册邮箱,邮箱有用户名和密码,一般会对其限制长度,这个限制长度的事情就是正则表达式做的

02正则表达式语法规则

* A: 正则表达式语法规则
	* a: 字符
		* x  代表的是字符x
		* \\ 代表的是反斜线字符'\'
		* \t 代表的是制表符
		* \n 代表的是换行符
		* \r 代表的是回车符
	* b: 字符类
		* [abc]    a、b 或 c(简单类)
		* [^abc]   任何字符,除了 a、b 或 c(否定)
		* [a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围) 
		* [0-9]    0到9的字符都包括
		* [a-zA-Z_0-9] 代表的字母或者数字或者下划线(即单词字符)
	* c: 预定义字符类
		* . 任何字符。
		* \d 数字:[0-9]
		* \w 单词字符:[a-zA-Z_0-9]如"com.itheima.tests"/finish
	* d: 边界匹配器
		* ^  代表的是行的开头
		* $  代表的是行的结尾
		* \b 代表的是单词边界
	* e: 数量词
		* X?     X,一次或一次也没有
		* X*     X,零次或多次
		* X+     X,一次或多次
		* X{n}   X,恰好 n 次 
		* X{n,}  X,至少 n 次 
		* X{n,m} X,至少 n 次,但是不超过 m 次

03正则表达式练习和相关的String类方法

* A: 正则表达式练习和相关的String类方法
	* a: boolean matches(String 正则的规则)
		* "abc".matches("[a]")  
		* 匹配成功返回true
	* b: String[] split(String 正则的规则)
		* "abc".split("a")  
		* 使用规则将字符串进行切割
	* c: String replaceAll( String 正则规则,String 字符串)
		* "abc0123".repalceAll("[\\d]","#")	
		* 按照正则的规则,替换字符串

04正则表达式匹配练习

* A: 正则表达式匹配练习
	* a: 案例代码
		public class RegexDemo {
			public static void main(String[] args) {
				checkTel();
			}
			
			
			/*
			 *  检查手机号码是否合法
			 *  1开头 可以是34578  0-9 位数固定11位
			 */
			public static void checkTel(){
				String telNumber = "1335128005";
				//String类的方法matches
				boolean b = telNumber.matches("1[34857][\\d]{9}");
				System.out.println(b);
			}
			
			/*
			 *  检查QQ号码是否合法
			 *  0不能开头,全数字, 位数5,10位
			 *  123456 
			 *  \\d  \\D匹配不是数字
			 */
			public static void checkQQ(){
				String QQ = "123456";
				//检查QQ号码和规则是否匹配,String类的方法matches
				boolean b = QQ.matches("[1-9][\\d]{4,9}");
				System.out.println(b);
			}
		}

05正则表达式切割练习

* A: 正则表达式切割练习
	* a: 案例代码
		public class RegexDemo1 {
			public static void main(String[] args) {
				split_1();
				split_2();
				split_3();
			}
			
			/*
			 * String类方法split对字符串进行切割
			 * 192.168.105.27 按照 点切割字符串
			 */
			public static void split_3(){
				String ip = "192.168.105.27";
				String[] strArr = ip.split("\\.");
				System.out.println("数组的长度"+strArr.length);
				for(int i = 0 ; i < strArr.length ; i++){
					System.out.println(strArr[i]);
				}
			}
			
			/*
			 * String类方法split对字符串进行切割
			 * 18 22 40 65 按照空格切割字符串
			 */
			public static void split_2(){
				String str = "18    22     40          65";
				String[] strArr = str.split(" +");
				System.out.println("数组的长度"+strArr.length);
				for(int i = 0 ; i < strArr.length ; i++){
					System.out.println(strArr[i]);
				}
			}
			
			/*
			 *  String类方法split对字符串进行切割
			 *  12-25-36-98  按照-对字符串进行切割
			 */
			public static void split_1(){
				String str = "12-25-36-98";
				//按照-对字符串进行切割,String类方法split
				String[] strArr = str.split("-");
				System.out.println("数组的长度"+strArr.length);
				for(int i = 0 ; i < strArr.length ; i++){
					System.out.println(strArr[i]);
				}
			}
		}		

06正则表达式替换练习

* A: 正则表达式替换练习
	* a: 案例代码
		public class RegexDemo1 {
			public static void main(String[] args) {
				replaceAll_1();
			}
			
			/*
			 * "Hello12345World6789012"将所有数字替换掉
			 * String类方法replaceAll(正则规则,替换后的新字符串)
			 */
			public static void replaceAll_1(){
				String str = "Hello12345World6789012";
				str = str.replaceAll("[\\d]+", "#");
				System.out.println(str);
			}
		}

07正则表达式邮箱地址验证

* A: 正则表达式邮箱地址验证
	* a: 案例代码
		public class RegexDemo2 {
			public static void main(String[] args) {
				checkMail();
			}
			/*
			 *  检查邮件地址是否合法
			 *  规则:
			 *   1234567@qq.com
			 *   mym_ail@sina.com
			 *   nimail@163.com
			 *   wodemail@yahoo.com.cn    
			 *   
			 *   @: 前  数字字母_ 个数不能少于1个
			 *   @: 后  数字字母     个数不能少于1个
			 *   .: 后面 字母 
			 *     
			 */
			public static void checkMail(){
				String email ="abc123@sina.com";
				boolean b = email.matches("[a-zA-Z0-9_]+@[0-9a-z]+(\\.[a-z]+)+");
				System.out.println(b);
			}
		}
			

附:个人电商中台开源项目

   square-pavilion(四方阁)

 链接:github.com/ChenJinchen…

  Square pavilion is a cube project for e-commerce.(四方阁是一个为电商而生的中台项目)。 在玄幻小说中,总有一些比较牛逼且左右逢源于各个势力之间的中立组织,这些组织通常带有“阁”字。因此本项目取名“四方阁”,取包容并蓄,吸取百家之长之意。欢迎有兴趣的小伙伴们一起努力,把这个项目完善、推广。 Github square-pavilion