正则表达式
正则表达式:正确规则的表达式,他是一个独立的语法,很多语言都支持,他的作用就是用来校验一段数据符不符合我们定义的规则。
用法:我们定义一个正则表达式,通过调用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)
获取正平方根