Java 正则表达式 Pattern和Matcher类 Math类 Random类 System类 BigDecimal类

14.01_正则表达式

  A:正则表达式:
  			正确规则的表达式,规则java给我们定的。指一个用来描述或者匹配一系
  			列符合某个句法规则的字符串的单个字符串。其实就是一种规则,有自己
  			特殊的应用。
  B:组成规则:
  			规则字符在java.util.regex Pattern类中

		   (1)字符
		   			x  字符x。 举例:'a'表示字符a
		   			\\ 反斜线字符。
		   			\n 新行(换行)符('\u000A')
		   			\r 回车符('\u000D')
		   (2)字符类
		   			[abc]    a、b或c(简单类)
		   			[^abc]   任何字符,除了a、b或c(否定)
		   			[a-zA-Z] a到z或A到Z,两头的字母包括在内(范围)
		   			[0-9]	 0到9的字符都包括
		   (3)预定义字符类
		   			.  任何字符,.字符本身用\.表示
		   			\d 数字:[0-9]
		   			\w 单词字符:[a-zA-Z_0-9],在正则表达式里面组成单词的
		   									  东西必须有这些东西组成
		   			\s 匹配空格字符
		   (4)边界匹配器
		   			^  行的开头
		   			$  行的结尾
		   			\b 单词边界 就是不是单词字符的地方
		   (5)Greedy数量词
		   			x?     x一次或一次也没有 比如""空串就是没有
		   			x*     x零次或多次 大于等于1次都算多次
		   			x+     x一次或多次
		   			x{n}   x恰好n次
		   			x{n,}  x至少n次
		   			x{n,m} x至少n次,但是不超过m次
  C:正则表达式的判断功能:
  			String类的功能:public boolean matches(String regex)
public class MyTest {
    public static void main(String[] args) {
         /* 需求:校验qq号码.
        1:要求必须是5 - 15 位数字
        2:0 不能开头*/
         Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的qq号:");
        String qqNum = sc.nextLine();
        //校验QQ号是否符合规则
        //boolean flag = checkQQNumber(qqNum);
        boolean flag1 = checkQQNumber1(qqNum);
    }

    private static boolean checkQQNumber1(String qqNum) {
        String regx = "[1-9][0-9]{4,14}";
        boolean b = qqNum.matches(regx);
        return b;
    }

    private static boolean checkQQNumber(String qqNum) {
        //定义一个标记
        boolean flag = false;
        if(qqNum.length() >= 5 && qqNum.length() <= 15 && !qqNum.startsWith("0")){
            //校验每一位是数字字符
            for (int i = 0; i < qqNum.length(); i++) {
                char ch = qqNum.charAt(i);
                if(!Character.isDigit(ch)){
                    return false;
                }else{
                    flag = true;
                }
            }
        }
        return flag;
    }
}
public class MyTest {
    public static void main(String[] args) {
        //正则表达式:正确规则的表达式,是一个独立语法,很多语言都会支持
        //			 正则表达式,
        // 主要作用:就是用来定义一些规则,进而校验数据是否符合正则表达式
        //			所描述的规则
        //Java中定义正则表达式可以使用字符串来定义
        // String regx="正则表达式的语法";
        //var regx=/正则表达式语法/ js中定义正则表达式的方式
        String regx = "abc";
        regx = "[a,b,c]";   //是否匹配我列表中所列举的字符a、b或c
        regx = "[a-z]";     //列举26个小写字母
        regx = "[A,B,C,D]";
        regx = "[a-zA-Z]";
        regx = "[^a-zA-Z]";
        regx = "[0,1,2]";
        regx = "[0-9]";
        regx = "[a-zA-Z0-9_%]";
        // . 可以匹配单个任意字符
        regx = ".";
        regx = "..";    //可以匹配两个任意字符
        // 只想要匹配点本身 需要转意
        regx = "\\.";
        // | 表示或者
        regx = "a|b";
        regx = "\\|";   //转意
        regx = "\\d";   //跟[0-9]意思一样
        regx = "\\w";   //\w单词字符:[a-zA-Z_0-9]
        regx = "\\D";   //跟[^0-9]意思一样
        regx = " ";
        regx = "\\s";   //匹配空格
        regx = "\\S";   //不是空格
        boolean b = "a".matches(regx);
        System.out.println(b);
    }
}
public class MyTest {
    public static void main(String[] args) {
        //+ 一次或多次
        String regx = "[0-9]+";
        regx = "a+b+";      //只能有1个或多个ab
        // * 零次或多次,大于等于1次都算多次
        regx = "a*";
        //? 一次或一次也没有 比如""空串 就是没有
        regx = "b?";
        regx = "\\w+";
        //正好几次
        regx = "a{5}";
        regx = "[0-9]{5}";
        // 次数大于等于5 小于等于15
        regx = "[0-9]{5,15}";
        //至少5位
        regx = "[0-9]{5,}";
        regx = "^a.+";      //^行的开头
        boolean b = "abcsdfsdfsdfsdf".matches(regx);
        System.out.println(b);
    }
}
public class MyTest3 {
    public static void main(String[] args) {
        //定义正则表达式
          /* 需求:校验qq号码.
        1:要求必须是5 - 15 位数字
        2:0 不能开头*/
        String regx = "[1-9][0-9]{4,14}";
        //这个数据是否匹配正则表达式
        boolean b = "asfasdf".matches(regx);

        //校验手机号的规则
        //手机号是 11位 每一位是数字 以1开头 第二位是 3 5 6 7 8 其余位是任意数字
        String phoneRegx = "[1][3,5,6,7,8][0-9]{9}";
        boolean f = "13992568959".matches(phoneRegx);
        System.out.println(f);

        //6~18个字符,可使用字母、数字、下划线,需要以字母开头
        //  westos@163.com   abcdhaha@163.com  abc123@163.com ab_c123@163.com
        //网易邮箱的规则
        String mailRegx = "[a-zA-Z]\\w{5,17}@163\\.com";
        boolean matches = "Westo_163s@163.com".matches(mailRegx);
        System.out.println(matches);

        String mailRegx2 = "[a-zA-Z]\\w{5,17}@[a-z]{2,16}\\.(com|cn|net|org)";

        //车牌号正则
        String car="[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}";

        //中文名字正则
        String chinese = "[\\u4e00-\\u9fa5]{2,4}";
        boolean name = "张三".matches(chinese);
        System.out.println(name);

        //可以是字母也可以中文名
        String username = "([a-zA-Z]{6,16})|([\\u4e00-\\u9fa5]{2,4})";
    }
}
  D:正则表达式的分割功能
  			split()方法:String类的功能:public String[] split(String regex)
public class MyTest {
    public static void main(String[] args) {
        //中文正则
        String chinese = "[\\u4e00-\\u9fa5]{2,4}";
        //matches(正则表达式);  判断该字符串的规则,是否符合正则表达式所
        //描述的规则。
        boolean matches1 = "王老虎".matches(chinese);
        System.out.println(matches1);

        //分割字符串
        String str = "username=张三=23=哈哈";
        /*String s= str.substring(0, str.indexOf("="));
        System.out.println(s);
        String s2 = str.substring(str.indexOf("=")+1);
        System.out.println(s2);*/
        String[] strings = str.split("=");
        System.out.println(Arrays.toString(strings));

        String str2 = "1111=asdfasdf2222=asdfasdfas3333=asdfasdfasdf44444";
        //根据你定义的分割符规则,来分割字符串
        String[] split = str2.split("[=a-z]+");
        System.out.println(Arrays.toString(split));
        for (int i = 0; i < split.length; i++) {
            System.out.println(split[i]);
        }
    }
}
public class MyTest {
    public static void main(String[] args) {
        /* A:
        案例演示:
        需求:我有如下一个字符串:”91 27 46 38 50”,请写代码实现最终输出
        结果是:”27 38 46 50 91”*/
        String str = "91      27    46 38 50    102 36 3 260 3 9";
        //1.截取字符串中的元素
        String[] strings = str.split("\\s+");
        //排序字符串数组,是按照字典顺序排序的;
        //Arrays.sort(strings);
        //System.out.println(Arrays.toString(strings));
        int[] arr = new int[strings.length];
        for (int i = 0; i < strings.length; i++) {
            arr[i] = Integer.parseInt(strings[i]);
        }
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));

        //遍历数组拼串
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            sb.append(arr[i]).append(" ");
        }
        String s = sb.toString().trim();
        System.out.println(s);
    }
}
  E:正则表达式的替换功能:
  			String类的功能:public String replaceAll(String regex,String replacement)
public class MyTest {
    public static void main(String[] args) {
        String str = "abc=====122255ccc====33352445477ddd";
        //String s = str.replace("=","");
        //System.out.println(s);
        //根据正则表达式来替换
        String s = str.replaceAll("[=0-9]+","=");
        System.out.println(s);
    }
}
public class MyTest {
    public static void main(String[] args) {
        String str = "da jia ting wo shuo, jin tian yao xia yu, bu shang wan zi xi, gao xing bu?";
        String s = str.replaceAll("[^a-z]","=");
        System.out.println(s);
        s = s.substring(0,s.lastIndexOf("="));
        System.out.println(s);
        String[] arr = s.split("=");
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < arr.length; i++){
            if(arr[i].length() == 3){
                System.out.println(arr[i]);
                sb.append(arr[i]).append(" ");
            }
        }
        String trim = sb.toString().trim();
        String[] arr2 = trim.split(" ");
        System.out.println(Arrays.toString(arr2));
    }
}
  F:Pattern和Matcher
public class MyTest {
    public static void main(String[] args) {
        //Pattern  模式器
        //Macher 匹配器
        //模式器:从来封装一个正则表达式
        Pattern p = Pattern.compile("a*b");
        //通过模式器获取匹配器,并且还要传入匹配的数据
        Matcher m = p.matcher("aaaaab");
        //调用匹配器中匹配的方法,进行匹配
        boolean b = m.matches();
        System.out.println(b);

        //如果我们仅仅只是想要知道一个字符串是否符合我们定义的正则,就调用
        //String类中的matches("a*b")方法
        boolean b1 = "aaaaab".matches("a*b");
    }
}
  G:正则表达式的获取功能
  			使用的是find()方法和group()方法,注意一定要先使用find()方法先
  			找到,才能用group()方法获取出来
public class MyTest {
    public static void main(String[] args) {
        //获取下面的字符串中是 三个字母组成的单词
        String str = "da jia ting wo shuo, jin tian yao xia yu, bu shang wan zi xi, gao xing bu?";
        //根据正则来获取
        String regx = "\\b[a-z]{3}\\b";

        //编译正则表达式
        Pattern p = Pattern.compile(regx);
        //获取匹配器,传入你要操作的数据
        Matcher m = p.matcher(str);

        /*  boolean find ()
        尝试查找与该模式匹配的输入序列的下一个子序列。

        String group ()
        返回由以前匹配操作所匹配的输入子序列。
        */
        //第一次查找,先find() 找到再 group()
        /*
        boolean b = m.find();
        if(b){
            String group = m.group();
            System.out.println(group);
        }
        //第二次找
        b = m.find();
        if(b){
            String group = m.group();
            System.out.println(group);
        }
        b = m.find();
        if (b) {
            String group = m.group();
            System.out.println(group);
        }
        */
        while(m.find()){
            String group = m.group();
            System.out.println(group);
        }
    }
}

14.02_Math类概述和方法使用

  A:Math类概述
  			Math类包含用于执行基本数学运算的方法,如初等指数、对数、平方根
  			和三角函数
  B:成员变量
  			public static final double E:				自然底数
  			public static final double PI:				圆周率
  C:成员方法
  			public static int abs(int a)				取绝对值
  			public static double ceil(double a)			向上取整
  			public static double floor(double a)		向下取整
  			public static int max(int a,int b)			获取最大值
  			public static int min(int a,int b)			获取最小值
  			public static double pow(double a,double b)	获取a的b次幂
  			public static double random()	获取随机数 返回带正号的double
  											值,该值大于等于0.0且小于1.0
  			public static int round(float a)			四舍五入
  			public static double sqrt(double a)			获取正平方根
public class MyTest {
    public static void main(String[] args) {
        /*
    成员变量
	public static final double E : 		自然底数
	public static final double PI:		圆周率
    成员方法
	public static int abs(int a)		取绝对值
	public static double ceil(double a)	向上取整
	public static double floor(double a)	向下取整
	public static int max(int a,int b)      获取最大值
	public static int min(int a, int b)	获取最小值
	public static double pow(double a,double b) 获取a的b次幂
	public static double random()	获取随机数 返回带正号的double值,该
									值大于等于 0.0 且小于 1.0。
	public static int round(float a) 四舍五入
	public static double sqrt(double a)获取正平方根
     */
        double pi = Math.PI;
        System.out.println(pi);

        System.out.println(Math.ceil(3.1));
        System.out.println(Math.floor(3.9));

        System.out.println(Math.max(3.5,6.9));
        int a = 20;
        int b = 60;
        int c = 100;
        System.out.println(Math.max((Math.max(a,b)),c));

        System.out.println(Math.pow(2,3));

        double sqrt = Math.sqrt(4);
        System.out.println(sqrt);
        //求8的立方根
        //public static double pow ( double a, double b)获取a的b次幂

        System.out.println(Math.pow(8,1/3.0));
    }
}

14.03_Random类

  A:Random类的概述
  		此类用于产生随机数如果用相同的种子创建两个Random实例,
  		则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。
  B:构造方法
  		public Random()				没有给定种子,使用的是默认的(当前系
  									统的毫秒值)
  		public Random(long seed)	给定一个long类型的种子,给定以后每一
  									次生成的随机数是相同的
  C:成员方法
  		public int nextInt()			没有参数 表示的随机数范围 是int
  										类型的范围
  		public int nextInt(int n)		可以指定一个随机数范围
  		void nextBytes(byte[] bytes)	生成随机字节并将其置于用户提供的
  										空的byte数组中。
public class MyTest {
    public static void main(String[] args) {
        Random random = new Random();
        for (int i1 = 0; i1 < 100; i1++) {
            //
            int num = random.nextInt(100);
            System.out.println(num);
        }
        /*  void nextBytes ( byte[] bytes)
        生成随机字节并将其置于用户提供的 byte 数组中。*/
        byte[] bytes = new byte[10];
        random.nextBytes(bytes);
        System.out.println(Arrays.toString(bytes));
    }
}
public class MyTest {
    public static void main(String[] args) {
        /* Random()
        创建一个新的随机数生成器。
        Random( long seed)
        使用单个 long 种子创建一个新的随机数生成器。
        */
        Random random = new Random(1L);
        for (int i = 0; i < 10; i++) {
            int i1 = random.nextInt(10);
            System.out.println(i1);
        }
    }
}

14.04_System类

  A:System类的概述
  		System类包含一些有用的类字段和方法。它不能被实例化
  B:成员方法
  		public static void gc()					调用垃圾回收器
  		public static void exit(int status)		退出java虚拟机,0为正常
  												退出,非0为异常退出
  		public static long currentTimeMillis()	获取当前时间的毫秒值
public class MyTest {
    public static void main(String[] args) {
        //in
        //public static final InputStream in“标准”输入流。此流已打开并
        //准备提供输入数据。通常,此流对应于键盘输入
        InputStream in = System.in;

        Scanner sc = new Scanner(System.in);

        /*  out
        public static final PrintStream out“标准”输出流。此流已打开并准
        备接受输出数据。通常,此流对应于显示器*/
        PrintStream out = System.out;
        out.println("abc");

        // System.out.println();
    }
}
public class MyTest {
    public static void main(String[] args) {
        //获取系统当前时间的毫秒值
        //获取的是从 1970 1 1 0:0:0 到 现在的一个时间间隔,单位是毫秒
        long start = System.currentTimeMillis();
        for (int i = 0; i < 5000; i++) {
            System.out.println(i);
        }
        long end = System.currentTimeMillis();
        // 1s=1000ms
        System.out.println("耗时:"+(end-start)+"毫秒");
    }
}
public class MyTest {
    public static void main(String[] args) {
        System.out.println("abc");
        System.out.println("abc");
        //退出虚拟机
        System.exit(0); //0 正常退出,非0强制退出
        System.out.println("abc");
        System.out.println("abc");
        System.out.println("abc");
        System.out.println("abc");
        System.out.println("abc");
        System.out.println("abc");
    }
}
public class MyTest {
    public static void main(String[] args) {
        Integer integer = new Integer(10);
        int[] ints = new int[20];
        //运行垃圾回收器
        System.gc();
        /*
        static String getenv(String name)
        获取指定的环境变量值。
         */

        String java_home = System.getenv("JAVA_HOME");
        System.out.println(java_home);

        String path = System.getenv("path");
        System.out.println(path);

        Properties properties = System.getProperties();
        Set<Object> objects = properties.keySet();
        for (Object key : objects) {
            System.out.println(key);
        }
        //user.home
        String property = System.getProperty("user.home");
        System.out.println(property);
        String property1 = System.getProperty("java.home");
        System.out.println(property1);
    }
}

14.05_BigDecimal类

  A:BigDecimal的概述
  			由于在运算的时候,float类型和double很容易丢失精度。所以为了能
  			精确的表示、计算浮点数,Java提供了BigDecimal
  			不可变的、任意精度的有符号十进制数。
  B:构造方法
  			public BigDecimal(String val)
  C:成员方法
  			public BigDecimal add(BigDecimal augend)			//加
  			public BigDecimal subtract(BigDecimal subtrahend)	//减
  			public BigDecimal multiply(BigDecimal multiplicand)	//乘
  			public BigDecimal divide(BigDecimal divisor)		//除法
  			public BigDecimal divide(BigDecimal divisor,int scale,int round,int roundingMode)
  			//scale小数点后面保留几位
  			//roundingMode 取舍模式 比如四舍五入
public class MyTest {
    public static void main(String[] args) {
        double a = 10;
        double b = 3;
        System.out.println(a/b);

        double c = 10.2525252;
        double d = 3.3696999999999999999999;
        System.out.println(c*d);

        BigDecimal one = new BigDecimal("3.369699999999999999999");
        BigDecimal two = new BigDecimal("10.2525252");
        BigDecimal multiply = one.multiply(two);
        System.out.println(multiply.toString());

        /*
        public BigDecimal add(BigDecimal augend)//加
	    public BigDecimal subtract(BigDecimal subtrahend)//减
	    public BigDecimal multiply(BigDecimal multiplicand)//乘
	    public BigDecimal divide(BigDecimal divisor)//除法
	    public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)//scale 小数点后面保留几位
	    // roundingMode 取舍模式 比如四舍五入
         */
    }
}
public class MyTest {
    public static void main(String[] args) {
        BigDecimal one = new BigDecimal("10");
        BigDecimal two = new BigDecimal("3");
        //除不尽,需要指定保留的位数和舍入模式
        BigDecimal divide = one.divide(two,30,BigDecimal.ROUND_FLOOR);
        System.out.println(divide);
    }
}
public class MyTest3 {
    public static void main(String[] args) {
        /*   long maxValue = Long.MAX_VALUE;
        long a=maxValue+1;
        System.out.println(a);
        */
        //BigInteger 用于超过long范围的计算
        long maxValue = Long.MAX_VALUE;
        BigInteger a = new BigInteger(maxValue + "");
        BigInteger b = new BigInteger("100");
        System.out.println(a.multiply(b));
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值