一.正则表达式
含义:
用来匹配或描述一系列符合某个语句规则的字符串
Pattern:代表了正则表达式的匹配模式
Matcher:提供了对正则表达式的分组支持,以及对正则表达式的多次匹配的支持
案例:
package com.dream.regex_class;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test01 {
public static void main(String[] args) {
//案例:把一个字符串中带电话号码替换成130****1111
String str = "小红13012341111 小绿15112342222";
//正则表达式的字符串
String regex = "(\\d{3})(\\d{4})(\\d{4})";
String replaceAll = str.replaceAll(regex, "$1****$3");
//底层原理:
// Pattern pattern = Pattern.compile(regex);//获取正则表达式的对象
// Matcher matcher = pattern.matcher(str);//匹配结果
// String replaceAll = matcher.replaceAll("$1****$3");//替换
System.out.println(replaceAll);
}
}
package com.dream.regex_class;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test02 {
public static void main(String[] args) {
//案例:校验QQ邮箱
String str = "1445584980@qq.com";
//正则表达式的字符串
String regex = "\\d{5,10}@qq.com";
boolean matches = str.matches(regex);
//底层原理:
// Pattern pattern = Pattern.compile(regex);//获取正则表达式对象
// Matcher matcher = pattern.matcher(str);//获取匹配结果
// boolean matches = matcher.matches();//判断是否完全匹配
System.out.println(matches);
}
}
package com.dream.regex_class;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test03 {
public static void main(String[] args) {
//案例:分隔路径
public static void main(String[] args) {
//分隔路劲
String str1 = "D:\\resource\\java\\正则表达式";
//String regex1 = "\\\\";
String regex1 = ":?\\\\";
String[] split = str1.split(regex1);
for (String string : split) {
System.out.println(string);
}
//底层
Pattern p1 = Pattern.compile(regex1);
String[] split2 = p1.split(str1);
}
}
package com.dream.regex_class;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test04 {
public static void main(String[] args) {
//案例:Pattern+Matcher 找到前端代码中的图片路径
String str = "<img src='hhy/aaa.jpg'/><div><div/> <input type='image' src='submit.gif' /><img src='bbb.jpg'/>";
//正则表达式的字符串
String regex = "<img\\b[^>]*\\bsrc\\b\\s*=\\s*('|\")?([^'\"\n\r\f>]+(\\.jpg)\\b)[^>]*>";
//获取正则表达式对象
Pattern pattern = Pattern.compile(regex);
//获取匹配结果的对象
Matcher matcher = pattern.matcher(str);
// System.out.println("在字符串中是否整个匹配:" + matcher.matches());
// System.out.println("在字符串中是否开头就匹配:" + matcher.lookingAt());
// System.out.println("在字符串中是否有包含匹配:" + matcher.find());
//遍历查找
while(matcher.find()){
String group = matcher.group(2);//获取匹配结果
System.out.println(group);
}
}
}
总结:
Pattern与Matcher一起合作
Matcher类提供了对正则表达式的分组支持,以及对正则表达式的多次匹配支持
单独用Pattern只能使用Pattern.matches(String regex,CharSequence input)一种最基础最简单的匹配。
经验:
1.正则表达式在工作中实际应用在验证邮箱、验证手机号码、替换字符串
2.正则表达式几乎不用我们自己写,百度即可
二.关于日期时间的类
1.Date类
代码实现:
package com.dream.datetime_class;
import java.util.Date;
public class Test01 {
public static void main(String[] args) {
Date date = new Date();
//星期 月份 日期 时:分:秒 时区 年份
//Thu Aug 05 11:23:00 CST 2021
System.out.println(date);
//自1970.1.1 0:0:0 往后推1000毫秒的时间
// Date date = new Date(1000);
//Thu Jan 01 08:00:01 CST 1970
// System.out.println(date);
}
}
2.SimpleDateFormat 格式化日期类
代码实现:
package com.dream.datetime_class;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test02 {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
//将Date 转 字符串
String format = sdf.format(new Date());
System.out.println(format);
///将字符串 转 Date
Date date = sdf.parse("2021年08月05日 11:28:61");
System.out.println(date);
}
}
3.Calendar日历类
代码实现:
package com.dream.datetime_class;
import java.util.Calendar;
public class Test03 {
public static void main(String[] args){
//获取日历类的对象
Calendar c = Calendar.getInstance();
//获取单个的日历信息
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH)+1;//月份0~11
int day = c.get(Calendar.DAY_OF_MONTH);
int hour = c.get(Calendar.HOUR);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
System.out.println(year);
System.out.println(month);
System.out.println(day);
System.out.println(hour);
System.out.println(minute);
System.out.println(second);
}
}
总结:
Date+SimpleDateFormat:获取一连串日期数据
Calendar:获取单个日期数据
三.Math类
含义:
Math 类提供了一序列基本数学运算和几何函数的方法。
Math类是final类,并且它的所有成员变量和成员方法都是静态的。
代码实现:
package com.dream.math_class;
public class Test01 {
public static void main(String[] args) {
System.out.println("求平方:" + Math.pow(3, 2));//9.0
System.out.println("求平方根:" + Math.sqrt(9));//3.0
System.out.println("求绝对值:" + Math.abs(-100));//100
System.out.println("向上取整(天花板):" + Math.ceil(1.001));//2.0
System.out.println("向下取整(地板):" + Math.floor(1.99));//1.0
System.out.println("求最大值:" + Math.max(10, 20));//20
System.out.println("求最小值:" + Math.min(10, 20));//10
System.out.println("四舍五入:" + Math.round(1.4));//1
System.out.println("获取随机值(0包含~1不包含):" + Math.random());
}
}
需求:
package com.dream.math_class;
public class Test02 {
public static void main(String[] args) {
//需求:随机出1~100的数字
System.out.println((int)(Math.random()*100) + 1);
}
}
//Math.abs() 有可能返回负数吗? 有可能
System.out.println(Math.abs(Integer.MAX_VALUE+1));//-2147483648
//原因:超过int类型数据的取值范围,导致二进制最高位(符号位)再加一
静态导入:
package com.dream.math_class;
//静态导入:将Math类中所有的静态属性和静态方法都导入Test04这个类中,把导入的静态属性和静态方法都认为是Test04自己的内容
import static java.lang.Math.*;
public class Test04 {
public static void main(String[] args) {
System.out.println("求平方:" + pow(3, 2));//9.0
System.out.println("求平方根:" + sqrt(9));//3.0
System.out.println("求绝对值:" + abs(-100));//100
System.out.println("向上取整(天花板):" + ceil(1.001));//2.0
System.out.println("向下取整(地板):" + floor(1.99));//1.0
System.out.println("求最大值:" + max(10, 20));//20
System.out.println("求最小值:" + min(10, 20));//10
System.out.println("四舍五入:" + round(1.4));//1
//静态导入缺点:可读性不高
//如果本类中有和静态导入类相同的方法,会就近调用本类中的方法
System.out.println("获取随机值(0包含~1不包含):" + random());//123456789
}
private static int random() {
return 123456789;
}
}
四.Random随机类
代码实现:
package com.dream.random_class;
import java.util.Random;
public class Test01 {
public static void main(String[] args) {
//创建随机类的对象
Random ran = new Random();
int nextInt1 = ran.nextInt();
System.out.println("随机出int取值范围内的数字:" + nextInt1);
int nextInt2 = ran.nextInt(10);
System.out.println("随机出0~9的数字:" + nextInt2);
boolean nextBoolean = ran.nextBoolean();
System.out.println("随机出boolean值:" + nextBoolean);
}
}
需求:
//点名器
package com.study.random_class;
import java.util.Random;
public class Test02 {
public static void main(String[] args) {
//点名器
Random random = new Random();
String[] names = {"刘洋","陶瑞","尹翔","张涛","李科","朱鉴","林成"};
for (int i = 0; i < 10000; i++) {
int index = random.nextInt(names.length);
System.out.println(names[index]);
}
}
}
深入Random:
package com.dream.random_class;
import java.util.Random;
public class Test03 {
public static void main(String[] args) {
//注意:随机全靠种子数,种子数固定,随机出的数据也是固定的
Random ran = new Random();
System.out.println(ran.nextInt());
System.out.println(ran.nextInt(100));
System.out.println("--------------");
MyRandom myRandom = new MyRandom();
System.out.println(myRandom.nextInt());
System.out.println(myRandom.nextInt(10));
}
}
//模拟底层实现
package com.dream.random_class;
public class MyRandom {
//种子数
private long seed;
public MyRandom() {
//seedUniquifier() ^ System.nanoTime() 获取到相对随机的种子数
this(seedUniquifier() ^ System.nanoTime());
}
public static long seedUniquifier(){
long current = System.currentTimeMillis();
for(;;){
current += current*5/2+3;
if(current%4==0 || current%7==0){
return current;
}
}
}
public MyRandom(long seed){
this.seed = seed;
}
public int nextInt(){
return (int) seed;
}
public int nextInt(int i){
return Math.abs((int) seed) % i;
}
}
五.Runtime类——运行环境类
package com.dream.runtime_class;
public class Test01 {
public static void main(String[] args) {
//获取运行环境对象
Runtime run = Runtime.getRuntime();
System.out.println("获取最大内存数(字节):" + run.maxMemory());
System.out.println("获取闲置内存数(字节):" + run.freeMemory());
System.out.println("获取处理数:" + run.availableProcessors());
}
}
应用:
package com.dream.runtime_class;
public class Test02 {
public static void main(String[] args) {
//测试程序效率(时间、内存)
// Runtime run = Runtime.getRuntime();
// long startTime = System.currentTimeMillis();
// long startMemory = run.freeMemory();
// String str = "小林";
// for (int i = 0; i < 50000; i++) {
// str += "小可爱,皇冠给你带";
// }
// long endMemory = run.freeMemory();
// long endTime = System.currentTimeMillis();
// System.out.println("消耗时长:" + (endTime-startTime));//5746
// System.out.println("消耗内存:" + (startMemory-endMemory));//-83835352
//获取自1970.1.1 0:0:0到现在的毫秒数
//获取当前闲置内存
Runtime run = Runtime.getRuntime();
long startTime = System.currentTimeMillis();
long startMemory = run.freeMemory();
StringBuilder sb = new StringBuilder("小林");
for (int i = 0; i < 50000; i++) {
sb.append("小可爱,皇冠给你带");
}
//获取运行后的闲置内存
long endMemory = run.freeMemory();
long endTime = System.currentTimeMillis();
System.out.println("消耗时长:" + (endTime-startTime));//5
System.out.println("消耗内存:" + (startMemory-endMemory));//2457664
}
}
六.System(系统类)属性
代码实现:
package com.dream.system_class;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;
public class Test01 {
public static void main(String[] args) {
//系统标准的输入流(方向:控制台 -> 程序)
InputStream in = System.in;
Scanner scan = new Scanner(in);
String next = scan.next();
//系统标准的输出流(方向:程序 -> 控制台)
// PrintStream out = System.out;
// out.println(next);
//系统标准的错误输出流(方向:程序 -> 控制台)
PrintStream err = System.err;
err.println(next);
//关闭资源
scan.close();
}
}
System的out和err
理解:
out和err是两个线程,谁抢到CPU资源就运行
多线程的程序随机性很强
代码实现:
package com.dream.system_class;
public class Test02 {
public static void main(String[] args) {
System.out.println("小明");
System.err.println("小红");
System.out.println("小强");
}
}
//输出结果随机
System的方法
代码实现:
package com.dream.system_class;
import java.util.Properties;
public class Test03 {
public static void main(String[] args) {
//获取系统参数的对象
Properties properties = System.getProperties();
System.out.println(properties);
//通过键获取值
String value = System.getProperty("os.name");//输出电脑操作系统名
System.out.println(value);
//退出当前虚拟机
System.exit(0);//退出后无法执行后面的程序
}
}