Java SE 17 API文档 Pattern
package day10.demo2;
/**
* @author NJR10byh
* @date 2022/7/27 14:13:28
* @description 工具类(校验常见的信息)
*/
public class validate {
private validate() {
}
// 检查手机号格式
public static boolean validatePhone(String str) {
if (str.matches("^((13[0-9])|(14[5-9])|(15[0-3[5-9]])|(16[6-7])|(17[1-8])|(18[0-9])|(19[1|3|5|6|8|9]))\\d{8}$"))
return true;
else return false;
}
// 检验邮箱格式
public static boolean validateEmail(String str) {
if (str.matches("^([a-zA-Z0-9]{1,30})@([a-zA-Z0-9]{2,20})(\\.[a-zA-Z0-9]{2,20}){1,2}$")) return true;
else return false;
}
// 校验电话格式
public static boolean validateTelePhone(String str) {
if (str.matches("^0\\d{2,3}-?\\d{5,10}$")) return true;
else return false;
}
}
调用
package day10.demo2;
import java.util.Scanner;
/**
* @author NJR10byh
* @date 2022/7/27 11:17:52
* @description 正则表达式
*/
public class task2_zhengze {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 检查手机号格式
while (true) {
System.out.print("请输入手机号码:");
String userPhone = scanner.nextLine();
if (validate.validatePhone(userPhone)) {
System.out.println("校验通过!");
break;
} else {
System.out.println("校验失败,请重试!");
}
}
// 检查邮箱格式
while (true) {
System.out.print("请输入邮箱:");
String userEmail = scanner.nextLine();
if (validate.validateEmail(userEmail)) {
System.out.println("校验通过!");
break;
} else {
System.out.println("校验失败,请重试!");
}
}
// 校验电话格式
while (true) {
System.out.print("请输入电话:");
String userTelePhone = scanner.nextLine();
if (validate.validateTelePhone(userTelePhone)) {
System.out.println("校验通过!");
break;
} else {
System.out.println("校验失败,请重试!");
}
}
}
}
package day10.demo3;
/**
* @author NJR10byh
* @date 2022/7/27 15:01:20
* @description 使用正则表达式分割数据
*/
public class task3 {
public static void main(String[] args) {
String names = "罗纳尔多saddsasaddle2312库里sda21dads樊振东";
String[] arrs = names.split("\\w+");
for (int i = 0; i < arrs.length; i++) {
System.out.println(arrs[i]);
}
String str = names.replaceAll("\\w+", " ");
System.out.println(str);
}
}
package day10.demo4;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author NJR10byh
* @date 2022/7/27 15:21:41
* @description 正则表达式爬取数据
*/
public class task4 {
public static void main(String[] args) {
String str = "来黑马程序学习Java,电话020-43422424,或者联系邮箱" +
"[email protected], 电话18762832633, 0203232323" +
"邮箱[email protected], 400-100-3233,4001003232";
String regex = "(((13[0-9])|(14[5-9])|(15[0-3[5-9]])|(16[6-7])|(17[1-8])|(18[0-9])|(19[1|3|5|6|8|9]))\\d{8})|"
+ "(([a-zA-Z0-9]{1,30})@([a-zA-Z0-9]{2,20})(\\.[a-zA-Z0-9]{2,20}){1,2})|"
+ "(0\\d{2,3}-?\\d{5,20})|"
+ "(400-?\\d{3,8}-?\\d{3,8})";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
/*
* 020-43422424
* [email protected]
* 18762832633
* 0203232323
* [email protected]
* 400-100-3233
* 4001003232
* */
}
}
package day10.demo5;
import java.util.Arrays;
/**
* @author NJR10byh
* @date 2022/7/27 15:54:43
* @description Arrays
*/
public class task5 {
public static void main(String[] args) {
int[] arr = {23, 4, 54, 43, 8, 10, 1};
System.out.println(Arrays.toString(arr));
// 排序
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
// 二分搜索技术(前提是数组必须排好序,否则报错)
int index = Arrays.binarySearch(arr, 43);
System.out.println(index);
// 返回不存在元素的规律:-(应该插入位置的下标 + 1)
int index2 = Arrays.binarySearch(arr, 45); // -(6 + 1)= -7
int index3 = Arrays.binarySearch(arr, 5); // -(2 + 1)= -3
System.out.println(index2);
System.out.println(index3);
}
}
package day10.demo6;
import java.util.Arrays;
import java.util.Comparator;
/**
* @author NJR10byh
* @date 2022/7/27 16:40:50
* @description 降序排列(自定义比较器)
*/
public class task6 {
public static void main(String[] args) {
int[] arr1 = {23, 4, 54, 43, 8, 10, 1};
Arrays.sort(arr1);
System.out.println(Arrays.toString(arr1));
Integer[] arr2 = {23, 4, 54, 43, 8, 10, 1};
Arrays.sort(arr2, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
/**
* 比较规则:
* o1 - o2 > 0:升序
* o2 - o1 > 0:降序
*/
return o2 - o1;
}
});
System.out.println(Arrays.toString(arr2));
}
}
package day10.demo7;
/**
* @author NJR10byh
* @date 2022/7/27 17:00:28
* @description Lambda表达式
*/
public class task7 {
public static void main(String[] args) {
Swimming s1 = new Swimming() {
@Override
public void swim() {
System.out.println("老师在游泳!");
}
};
go(s1);
System.out.println("-----------------");
// Lambda表达式
Swimming s2 = () -> System.out.println("学生在游泳!");
go(s2);
}
public static void go(Swimming s) {
System.out.println("开始");
s.swim();
System.out.println("结束");
}
}
@FunctionalInterface
interface Swimming {
void swim();
}
练习代码: day10_code