常用正则表达式记录

正则表达式在线测试——菜鸟工具:https://c.runoob.com/front-end/854/

1. 密码校验

^.*(?=.{
    
    8,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[~!@#$%^&*()_+|<>,.?/:;'\[\]{}" ]).*$
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexMatches {
    
    
	
	public static void main(String args[]) {
    
    
		String password= "12345678!Qass";
		String pattern = "^.*(?=.{8,})(?=.*\\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[~!@#$%^&*()_+|<>,.?/:;'\\[\\]{}\" ]).*$";

		Pattern r = Pattern.compile(pattern);
		Matcher m = r.matcher(password);
		System.out.println(m.matches());
	}

}

在这里插入图片描述

2. 电话号码校验(支持手机号码,3-4位区号,7-8位直播号码,1-4位分机号)

((\d{
    
    11})|^((\d{
    
    7,8})|(\d{
    
    4}|\d{
    
    3})-(\d{
    
    7,8})|(\d{
    
    4}|\d{
    
    3})-(\d{
    
    7,8})-(\d{
    
    4}|\d{
    
    3}|\d{
    
    2}|\d{
    
    1})|(\d{
    
    7,8})-(\d{
    
    4}|\d{
    
    3}|\d{
    
    2}|\d{
    
    1}))$)
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexMatches {
    
    
	
	public static void main(String args[]) {
    
    
		String str = "17755555555";
		String pattern = "((\\d{11})|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d)|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d))$)";

		Pattern r = Pattern.compile(pattern);
		Matcher m = r.matcher(str);
		System.out.println(m.matches());
	}

}

3. 座机电话号码校验(0530-2536987,021-87888822)

\d{
    
    3}-\d{
    
    8}|\d{
    
    4}-\d{
    
    7}
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexMatches {
    
    
	
	public static void main(String args[]) {
    
    
		String str = "0530-2536987";
		String pattern = "\\d{3}-\\d{8}|\\d{4}-\\d{7}";

		Pattern r = Pattern.compile(pattern);
		Matcher m = r.matcher(str);
		System.out.println(m.matches());
	}

}

4. 邮箱校验

^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexMatches {
    
    
	
	public static void main(String args[]) {
    
    
		String str = "[email protected]";
		String pattern = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";

		Pattern r = Pattern.compile(pattern);
		Matcher m = r.matcher(str);
		System.out.println(m.matches());
	}

}

5. 更多正则表达式参考网站

    1. 菜鸟工具: https://c.runoob.com/front-end/854/
    1. 菜鸟教程:https://www.runoob.com/regexp/regexp-example.html
  • 在这里插入图片描述
    1. GitHub仓库: https://github.com/Janson404/learn-regex-zh

猜你喜欢

转载自blog.csdn.net/qq_42102911/article/details/129005145