Java--正则表达式

一、正则表达式:是指一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串。
      
其实就是一种规则。有自己特殊的应用。
       作用:比如注册邮箱,邮箱有用户名和密码,一般会对其限制长度,这个限制长度的事情就是正则表达式做的

二、字符类
(1)[abc] a、b 或 c(简单类)
(2)[^abc] 任何字符,除了 a、b 或 c(否定)
(3)[a-zA-Z] a 到 z 或 A 到 Z,两头的字母包括在内(范围)
(4)[a-d[m-p]] a 到 d 或 m 到 p:[a-dm-p](并集)
(5)[a-z&&[def]] d、e 或 f(交集)
(6)[a-z&&[^bc]] a 到 z,除了 b 和 c:[ad-z](减去) 
(7)[a-z&&[^m-p]] a 到 z,而非 m 到 p:[a-lq-z](减去)
(8)[0-9] 0到9的字符都包括
注意:[]中判断的是单个字符,如“10”就属于两个字符‘1’和‘0’

三、预定义字符类
(1). 任何字符(与行结束符可能匹配也可能不匹配)
(2)\d 数字:[0-9]
(3)\D 非数字: [^0-9] 
(4)\s 空白字符:[ \t\n\x0B\f\r] 

(5)\S 非空白字符:[^\s] 
(6)\w 单词字符:[a-zA-Z_0-9] 
(7)\W 非单词字符:[^\w] 
四、Greedy 数量词
    (1)X? X,一次或一次也没有
    (2)X* X,零次或多次
    (3)X+ X,一次或多次
    (4)X{n} X,恰好 n 次
    (5)X{n,} X,至少 n 次
    (6)X{n,m} X,至少 n 次,但是不超过 m 次 

五、正则表达式的分割功能
1、正则表达式的分割功能
            String类的功能:public String[] split(String regex)
2、正则表达式的替换功能
            String类的功能:public String replaceAll(String regex,String replacement)

import java.util.Arrays;

public class regularQQ {

	public static void main(String[] args) {
		/*
		 * 一、正则表达式:是指一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串。
		                      其实就是一种规则。有自己特殊的应用。
	                          作用:比如注册邮箱,邮箱有用户名和密码,一般会对其限制长度,这个限制长度的事情就是正则表达式做的
	                  二、字符类
				(1)[abc] a、b 或 c(简单类) 
				(2)[^abc] 任何字符,除了 a、b 或 c(否定) 
				(3)[a-zA-Z] a 到 z 或 A 到 Z,两头的字母包括在内(范围) 
				(4)[a-d[m-p]] a 到 d 或 m 到 p:[a-dm-p](并集) 
				(5)[a-z&&[def]] d、e 或 f(交集) 
				(6)[a-z&&[^bc]] a 到 z,除了 b 和 c:[ad-z](减去) 
				(7)[a-z&&[^m-p]] a 到 z,而非 m 到 p:[a-lq-z](减去) 
				(8)[0-9] 0到9的字符都包括
			     注意:[]中判断的是单个字符,如“10”就属于两个字符‘1’和‘0’
		       三、预定义字符类 
                (1). 任何字符(与行结束符可能匹配也可能不匹配) 
				(2)\d 数字:[0-9] 
				(3)\D 非数字: [^0-9] 
				(4)\s 空白字符:[ \t\n\x0B\f\r] 
				(5)\S 非空白字符:[^\s] 
				(6)\w 单词字符:[a-zA-Z_0-9] 
				(7)\W 非单词字符:[^\w] 
		      四、Greedy 数量词 
				(1)X? X,一次或一次也没有 
				(2)X* X,零次或多次 
				(3)X+ X,一次或多次 
				(4)X{n} X,恰好 n 次 
				(5)X{n,} X,至少 n 次 
				(6)X{n,m} X,至少 n 次,但是不超过 m 次 
		      五、正则表达式的分割功能
		     1、正则表达式的分割功能
		          String类的功能:public String[] split(String regex)
		     2、正则表达式的替换功能
	              String类的功能:public String replaceAll(String regex,String replacement)
		 * */
		//校验QQ5~15位不能以0开头的数字QQ
		System.out.println(checkQQ("gud78318"));
		System.out.println(checkQQ("078318"));
		System.out.println(checkQQ("5648318"));
		
		System.out.println("------------");
		//正则表达式,matches方法匹配
		String regx = "[1-9]\\d{4,14}";
		System.out.println("5648318".matches(regx));
		
		System.out.println("===============排序===============");
		//排序
		String s = "22 11 33 55 44";
		String[] sA = s.split(" ");
		int[] iA = new int[sA.length];
		for(int i = 0;i < iA.length;i++){
			iA[i] = Integer.parseInt(sA[i]);  //parseInt方法将int转成整形字符串
			//每个基本数据类型都有一个parseXXX方法直接将本类型数据转为该类型字符串
		}
		Arrays.sort(iA);
		for (int i = 0;i < iA.length;i++){
			System.out.print(iA[i] + " ");
		}
	}

	private static boolean checkQQ(String QQ) {
		boolean flag = true;
		if (QQ.length() >= 0 && QQ.length() <= 9) {
			if (!QQ.startsWith("0")){
				char[] cA = QQ.toCharArray();
				for(int i =0;i < cA.length;i++){
					if(!(cA[i] >= '0' && cA[i] <= '9')){
						flag = false;
					}
				}
			}else{
				flag = false;
			}
		}else{
			flag = false;
		}
		return flag;
	}
}


 六、Math 类:包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。 

成员方法

        [1]public static int abs(int a)                      取绝对值

        [2]public static double ceil(double a) 向上取值

        [3]public static double floor(double a) 向下取值    

        [4]public static int max(int a,int b)  取两个数中最大值

        [5]public static double pow(double a,double b)       次方(前面底数,后面指数)

        [6]public static double random() 随机数(0.0~1.0,包括0.0,不包括1.0)

        [7]public static int round(float a)  四舍五入

        [8]public static double sqrt(double a) 开平方

七、Random类的概述

       此类用于产生随机数如果用相同的种子创建两个 Random 实例,

       则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。

1、构造方法

       [1]public Random()

       [2]public Random(long seed)

2、成员方法

       [1]public int nextInt()

       [2]public int nextInt(int n)生成0~n-1之间的数,不包括n

八、System类的概述

       System 类包含一些有用的类字段和方法。它不能被实例化。 

成员方法

       [1]public static void gc()                运行垃圾回收器

       [2]public static void exit(int status)    非0状态是异常终止,退出Java虚拟机

       [3]public static long currentTimeMillis() 获取当前时间的毫秒值

       [4]pubiic static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)   复制数组

九、BigInteger的概述

       可以让超过Integer范围内的数据进行运算

1、构造方法

       public BigInteger(String val)

2、成员方法

       [1]public BigInteger add(BigInteger val)                 +

       [2]public BigInteger subtract(BigInteger val) -

       [3]public BigInteger multiply(BigInteger val)            *

       [4]public BigInteger divide(BigInteger val) /

       [5]public BigInteger[] divideAndRemainder(BigInteger val)   取除数和余数

十、BigDecimal的概述

       由于在运算的时候,float类型和double很容易丢失精度,为了能精确的表示、计算浮点数,Java提供了BigDecimal

        不可变的、任意精度的有符号十进制数。

1、构造方法

public BigDecimal(String val)

2、成员方法

        [1]public BigDecimal add(BigDecimal augend)

        [2]public BigDecimal subtract(BigDecimal subtrahend)

        [3]public BigDecimal multiply(BigDecimal multiplicand)

        [4]public BigDecimal divide(BigDecimal divisor)

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Random;

public class MathMethod {

	public static void main(String[] args) {
		/*一、Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。 
             成员方法
				[1]public static int abs(int a)                      取绝对值
				[2]public static double ceil(double a)				向上取值
	 			[3]public static double floor(double a)				向下取值
				[4]public static int max(int a,int b) 				取两个数中最大值
				[5]public static double pow(double a,double b)       次方(前面底数,后面指数)
				[6]public static double random()						随机数(0.0~1.0,包括0.0,不包括1.0)
				[7]public static int round(float a) 					四舍五入
				[8]public static double sqrt(double a)				开平方
		  二、Random类的概述
				此类用于产生随机数如果用相同的种子创建两个 Random 实例,
				则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。
			 1、构造方法
				[1]public Random()
				[2]public Random(long seed)
			 2、成员方法
				[1]public int nextInt()
				[2]public int nextInt(int n)生成0~n-1之间的数,不包括n
		  三、System类的概述
				System 类包含一些有用的类字段和方法。它不能被实例化。 
			 成员方法
				[1]public static void gc()                运行垃圾回收器
				[2]public static void exit(int status)    非0状态是异常终止,退出Java虚拟机
				[3]public static long currentTimeMillis() 获取当前时间的毫秒值
				[4]pubiic static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)   复制数组
		  四、BigInteger的概述
				可以让超过Integer范围内的数据进行运算
			 1、构造方法
				public BigInteger(String val)
			 2、成员方法
				[1]public BigInteger add(BigInteger val)                 +
				[2]public BigInteger subtract(BigInteger val)			-
				[3]public BigInteger multiply(BigInteger val)            *
				[4]public BigInteger divide(BigInteger val)				/
				[5]public BigInteger[] divideAndRemainder(BigInteger val)   取除数和余数
		  五、BigDecimal的概述
				由于在运算的时候,float类型和double很容易丢失精度,为了能精确的表示、计算浮点数,Java提供了BigDecimal
				不可变的、任意精度的有符号十进制数。
			 1、构造方法
				public BigDecimal(String val)
			 2、成员方法
				[1]public BigDecimal add(BigDecimal augend)
				[2]public BigDecimal subtract(BigDecimal subtrahend)
				[3]public BigDecimal multiply(BigDecimal multiplicand)
				[4]public BigDecimal divide(BigDecimal divisor)
		 * */
		mathMethod();
		randomMethod();
		systemMethod();
		bigInteger();
		bigDecimal();
	}

	private static void bigDecimal() {
		System.out.println(2.0-1.1);
		BigDecimal b1 = new BigDecimal(2.0);        //这种方式依然不够精确
		BigDecimal b2 = new BigDecimal(1.1);
		System.out.println(b1.subtract(b2));
		
		BigDecimal b3 = new BigDecimal("2.0");      //这种方式也较为精确
		BigDecimal b4 = new BigDecimal("1.1");
		System.out.println(b3.subtract(b4));
		
		BigDecimal b5 = BigDecimal.valueOf(2.0);    //这种方式也较为精确
		BigDecimal b6 = BigDecimal.valueOf(1.1);
		System.out.println(b5.subtract(b6));
	}

	private static void bigInteger() {
		BigInteger b1 = new BigInteger("100");
		BigInteger b2 = new BigInteger("2");
		System.out.println(b1.add(b2));
		System.out.println(b1.subtract(b2));
		System.out.println(b1.multiply(b2));
		System.out.println(b1.divide(b2));
		
		BigInteger[] arr = b1.divideAndRemainder(b2);
		for(int i = 0;i < arr.length;i++){
			System.out.print(arr[i] + "----");
		}
		System.out.println();
		System.out.println("--------");
	}

	private static void systemMethod() {
		System.out.println("================================");
		//垃圾回收
		for(int i = 0; i < 2;i++){
			new Demo();
			System.gc();
		}
		
//		System.exit(0);     //非0状态是异常终止,退出Java虚拟机
//		System.out.println("11112222");
		
		//当前毫秒值
		long start = System.currentTimeMillis();
		for(int i = 0;i < 100000;i++){
			System.out.print("");
		}
		long end = System.currentTimeMillis();
		System.out.println("循环10万次使用了"+(end-start)+"毫秒");
		
		//数组拷贝
		int[] src = {11,22,33,44,55};
		int[] dest = new int[8];
		for(int i = 0;i<dest.length;i++){
			System.out.print(dest[i] + " ");
		}
		System.out.println();
		System.out.println("--------");
		System.arraycopy(src, 0, dest, 0, src.length);
		for(int i = 0;i<dest.length;i++){
			System.out.print(dest[i] + " ");
		}
		System.out.println();
		System.out.println("--------");
	}

	private static void randomMethod() {
		System.out.println("================================");
		Random r = new Random();
		for(int i = 0; i < 10; i++) {
			//System.out.println(r.nextInt());
			System.out.println(r.nextInt(100));			//生成0~99之间的数,不包括100
		}
        Random r2 = new Random(1001);
		
		int a = r2.nextInt();
		int b = r2.nextInt();
		
		System.out.println(a);
		System.out.println(b);
	}

	private static void mathMethod() {
				/*
				 * 13.0
				 * 12.3
				 * 12.0
				 */
				System.out.println(Math.ceil(12.3));			
				System.out.println(Math.ceil(12.99));
				
				System.out.println("-----------");
				//floor
				/*
				 * 13.0
				 * 12.3
				 * 12.0
				 */
				System.out.println(Math.floor(12.3));			
				System.out.println(Math.floor(12.99));
				
				System.out.println(Math.max(20, 30));
				
				System.out.println(Math.pow(2, 3));				//2.0 ^ 3.0
				
				System.out.println(Math.random());
				
				System.out.println(Math.round(12.3f));
				System.out.println(Math.round(12.9f));
				
				System.out.println(Math.sqrt(4));
				System.out.println(Math.sqrt(2));
				System.out.println(Math.sqrt(3));
	}
}

class Demo{           //在一个源文件中只能存在一个使用public修饰的类
	@Override
	protected void finalize(){
		System.out.println("垃圾被清理了");
	}
}
十一、PatternMatcher

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PatternMethod {
	public static void main(String[] args) {
		Pattern p = Pattern.compile("a*b");      //获取正则表达式
		Matcher m = p.matcher("aaab");           //获取匹配器
		boolean b = m.matches();					//看是否能匹配
		System.out.println(b);
		System.out.println(Pattern.compile("a*b").matcher("aaab").matches());  //结果一致
		
		checkMobile();
		
		//正则表达式的分组功能
		regex1();
		regex2();
		regex3();
		regex4();
	}

	private static void checkMobile() {
		System.out.println("--------------------");
		String s = "我现在手机18762988956,之前手机13233888956,最初手机13889568899";
		String regx = "1[3578]\\d{9}";
		Pattern p = Pattern.compile(regx);
		Matcher m = p.matcher(s);
		while(m.find()){
			System.out.println(m.group());
		}
	}

	private static void regex4() {
		String s = "我我....我...我.要...要要....学学..学.编..编编.编.程.程.程..程";//输出:我要学编程
		String s1 = s.replaceAll("\\.+", "");
		String s2 = s1.replaceAll("(.)\\1+", "$1"); //$1代表第一组中的内容
		System.out.println(s1);
		System.out.println(s2);
	}
	private static void regex3() {
		System.out.println("-------------------");
		String s = "edqqwdgggvcaaaadg";//以叠词切割
		String regex = "(.)\\1+";     //+代表出现第一组出现一次到多次
		String[] sA = s.split(regex);
		for (int i = 0;i < sA.length; i++){
			System.out.println(sA[i]);
		}
	}
	private static void regex2() {
		System.out.println("----------两个连叠词---------");
		String regex = "(..)\\1"; 
		System.out.println("懒懒散散".matches(regex));
		System.out.println("快快乐乐".matches(regex));
		System.out.println("死啦死啦".matches(regex));
	}

	private static void regex1() {
		String regex1 = "(.)\\1(.)\\2";     //\\1代表第一组又出现一次,\\2代表第二组又出现一次
		System.out.println("懒懒散散".matches(regex1));
		System.out.println("快快乐乐".matches(regex1));
		System.out.println("死啦死啦".matches(regex1));
	}
}


你可能感兴趣的:(正则表达式)