正则表达式和常用类

正则表达式

正则表达式:正确规则的表达式,他是一个独立的语法,很多语言都支持,他的作用就是用来校验一段数据符不符合我们定义的规则。
用法:我们定义一个正则表达式,通过调用matches()方法返回一个布尔类型的数据,可以判断数据符不符合我们规定的正则表达式。括号里面传入的就是我们所规定的正则表达式。
例如:规定用户名由大小写字数和数字组成,并且是6-16位。

import java.util.Scanner;

public class Demo6 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String s = scanner.nextLine();
        String regx="[a-z0-9A-z]{6,16}";
        boolean matches = s.matches(regx);
        if(matches){
            System.out.println("用户名合法");
        }else {
            System.out.println("用户名不合法");
        }
    }
}

正则表达式的规则:
regx = “[abcdef]”; //运行出现列表中的某一个字符
regx = “[a-z]”;//允许出现26个小写字母的任意一个
regx = “[A-Za-z]”;
regx = “[0-9]”;
regx = “[a-zA-Z0-9]”;
regx = “[^0-9]”; //把不允许出现我列表中的任意一个
regx = “.”;//通配单个任意字符
regx = “\.”;// \ 转意字符 \. 这个意思就是匹配点本身
regx = “\|”;
regx = “\d”;//“跟”[0-9]意思一样
regx = “\w”;//跟 [a-zA-Z_0-9] 意思一样
regx = “[a-z]+”; //+ 可以出现一个或多个
regx = “a?”; //一次或一次也没有 比如 “” 空串 就是没有
regx = “[a-z]*”;//零次或多次 大于等于1次 都算多次
regx = “[a-z]{6}”;//正好6次
regx = “[0-9]{6,}”;//至少6个
regx = “[0-9]{6,16}”;//大于等于 6 小于等于16
方法:

  • public boolean matches(String regex)
    用来判断一段数据是否符合正则并返回一个布尔类型
  • public String[] split(String regex)
    在一段数据中截取不符合正则的截取出来,返回一个字符串类型数组
  • public String replaceAll(String regex,String replacement)
    将一段数据中符合正则的字符串替换成新的字符串
Math类

这类 Math包含用于执行基本的数字运算等基本指数、对数、平方根法、三角函数。

方法:

  • 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()
    获取随机数,值在0.0和1.0之间
  • public static int round(float a)
    四舍五入
  • public static double sqrt(double a)
    获取正平方根
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值