//获取一个模式器
Pattern p = Pattern.compile("a*b*");//通过模式器获取一个匹配器
Matcher m = p.matcher("aaaaab");//进行匹配boolean b = m.matches();
System.out.println(b);//true
Matcher匹配器的方法:
boolean find ()
尝试查找与该模式匹配的输入序列的下一个子序列。
String group ()
返回由以前匹配操作所匹配的输入子序列。
举例:
import java.util.regex.Matcher;import java.util.regex.Pattern;publicclassBlog3{publicstaticvoidmain(String[] args){//把下面这个字符串中是三个字母组成的单词,获取出来// da jia ting wo shuo, jin tian yao xia yu, bu shang wan zi xi, gao xing bu?
String str="da jia ting wo shuo, jin tian yao xia yu, bu shang wan zi xi, gao xing bu?";//定义三个字母单词的正则表达式 注意加单词边界,否则是三个字符的都会取到
String strRegx="\\b[a-z]{3}\\b";//把正则表达式封装到模式器中
Pattern p = Pattern.compile(strRegx);//获取匹配器,把待匹配的字符串传进去
Matcher matcher = p.matcher(str);while(matcher.find()){//判断下一个字符词组是否为三个字母组成的单词
String group = matcher.group();//获取匹配上的单词
System.out.println(group.concat(" "));}}}
运行结果:
jia jin yao xia wan gao
Process finished with exit code 0
三、Math类概述和方法使用
A:Math类概述
Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
B: 成员变量
publicstaticfinaldouble E : 自然底数
publicstaticfinaldouble PI: 圆周率
C:成员方法
publicstaticintabs(int a) 取绝对值
publicstaticdoubleceil(double a) 向上取整
publicstaticdoublefloor(double a) 向下取整
publicstaticintmax(int a,int b) 获取最大值
publicstaticintmin(int a,int b) 获取最小值
publicstaticdoublepow(double a,double b) 获取a的b次幂
publicstaticdoublerandom() 获取随机数返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。
publicstaticintround(float a) 四舍五入
publicstaticdoublesqrt(double a) 获取正平方根
演示:
publicclassMathDemo{publicstaticvoidmain(String[] args){//Math 数学工具类//属性double pi = Math.PI;double e = Math.E;//方法double num = Math.random();//生成double类型随机数 0-----1 之间//获取最值int max = Math.max(19,20);//20int min = Math.min(20,40);//20//向上取整,向下取整double ceil = Math.ceil(1.9);//2.0double floor = Math.floor(1.9);//1.0//四舍五入long round = Math.round(1.48);//小数点后一位四舍五入 结果:1//求一个数的几次幂double pow = Math.pow(2,3);//2的3次方//求平方根double sqrt = Math.sqrt(4);//2.0//求一个数的立方根double pow2 = Math.pow(8,1.0/3);//2.0//求一个数的绝对值int abs = Math.abs(-1);//1}}
四、Random类
A:Random类的概述
此类的实例用于生成伪随机数流,使用48位的种子,如果用相同的种子创建两个 Random 实例,
则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。
B:构造方法
public Random() 没有给定种子,使用的是默认的(当前系统的毫秒值)
public Random(long seed) 给定一个long类型的种子,给定以后每一次生成的随机数是相同的
C:成员方法
public int nextInt()//没有参数 表示的随机数范围 是int类型的范围
public int nextInt(int n)//可以指定一个随机数范围
void nextBytes(byte[] bytes) 生成随机字节并将其置于用户提供的空的 byte 数组中。
示例:
publicclassRandomDemo{publicstaticvoidmain(String[] args){//生成随机数据的类// 此类的实例用于生成伪随机数流。此类使用 48 位的种子,
Random random =newRandom();//生成一些随机整数
random.nextInt();//如果没有传入一个范围,生成的随机数就是int的范围
random.nextInt(100);//范围是0-99,包含0和99
random.nextDouble();//生成一个随机double范围内的数
random.nextBoolean();//生成一个随机boolean值byte[] bytes=newbyte[10];//随机一些byte范围的数,填到这个数组中
random.nextBytes(bytes);show();show();show();}publicstaticvoidshow(){int[] arr=newint[10];//Random( long seed)//使用单个 long 种子创建一个新的随机数生成器。//给定了种子,每次生成的随机数都是一样的
Random random =newRandom(1);for(int i =0; i <10; i++){int i1 = random.nextInt(10);
arr[i]=i1;}
System.out.println(Arrays.toString(arr));}}
运行结果:
[5,8,7,3,4,4,4,6,8,8][5,8,7,3,4,4,4,6,8,8][5,8,7,3,4,4,4,6,8,8]
Process finished with exit code 0