正则表达式 手机号、密码、验证码

package com.wck;

import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.regex.Pattern;

@SpringBootTest
public class PatternDemoTest {


    /*  要使用正则表达式验证账号格式,
           满足以下条件:4-20位,仅支持汉字、数字、字母、减号(-)和下划线(_)。
           ^ 表示字符串的开始
           [\\u4e00-\\u9fa5] 匹配任意一个汉字
           \\w 匹配任意一个字母、数字或下划线
           - 匹配减号
           {4,20} 表示长度在4到20之间
           $ 表示字符串的结束
        */
    @Test
    public void patternNameTest() {

        String regex = "^[\\u4e00-\\u9fa5\\w-]{4,20}$";
        boolean account = Pattern.matches(regex, "account");
        System.out.println("是否满足 account = " + account);

        System.out.println("是否满足 account = " + Pattern.matches(regex, "A满-_"));
        System.out.println("是否满足 account = " + Pattern.matches(regex, "-123"));
        //false
        System.out.println("是否满足 account = " + Pattern.matches(regex, "&444"));
        System.out.println("是否满足 account = " + Pattern.matches(regex, "44"));

    }

    /**
     * 密码8-20位,仅支持汉字、数字,并且必须符合两种以上的组合
     * ^ 表示字符串的开始
     * (?=.*[\\u4e00-\\u9fa5]) 表示必须包含至少一个汉字
     * (?=.*\\d) 表示必须包含至少一个数字
     * [\\u4e00-\\u9fa5\\d] 匹配任意一个汉字或数字
     * {8,20} 表示长度在8到20之间
     * $ 表示字符串的结束
     */
    @Test
    public void patternPassWordTest() {

        String regex = "^(?=.*[\\u4e00-\\u9fa5])(?=.*\\d)[\\u4e00-\\u9fa5\\d]{8,20}$";
        System.out.println("是否满足 account = " + Pattern.matches(regex, "汉字123456"));
        //false
        System.out.println("是否满足 account = " + Pattern.matches(regex, "123汉字"));
        System.out.println("是否满足 account = " + Pattern.matches(regex, "abc123"));
        System.out.println("是否满足 account = " + Pattern.matches(regex, "abc汉字"));

    }


    /**
     * 手机号11位数字
     * ^ 表示字符串的开始
     * 1 表示手机号码必须以数字1开头
     * \\d{10} 表示接下来的10个字符必须是数字
     * $ 表示字符串的结束
     */
    @Test
    public void patternPhoneTest() {

        String regex = "^1\\d{10}$";

        System.out.println(Pattern.matches(regex, "13812345678"));
        System.out.println(Pattern.matches(regex, "12345678901"));
        System.out.println(Pattern.matches(regex, "13812345"));

    }


    /**
     * 6位数字的验证码
     */
    @Test
    public void patternCodeTest() {

        String regex = "^\\d{6}$";

        System.out.println(Pattern.matches(regex, "345678"));
        //false
        System.out.println(Pattern.matches(regex, "78901"));
        System.out.println(Pattern.matches(regex, "eww345"));

    }

    @Test
    public void isBlank() {

        String str1 = " ";
        String str2 = "";
        String str3 = null;

        System.out.println("str1 = " + StringUtils.isBlank(str1));
        System.out.println("str1 = " + StringUtils.isEmpty(str1));

        System.out.println("str2 = " + StringUtils.isBlank(str2));
        System.out.println("str2 = " + StringUtils.isEmpty(str2));

        System.out.println("str3 = " + StringUtils.isBlank(str3));
        System.out.println("str3 = " + StringUtils.isEmpty(str3));

    }


}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解答这个问题。 首先,我们可以使用 PHP 的 preg_match() 函数进行正则表达式匹配。以下是用于验证邮箱地址、密码手机号验证码正则表达式: 邮箱地址: ``` /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ ``` 密码: ``` /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/ ``` 手机号: ``` /^1[3456789]\d{9}$/ ``` 验证码: ``` /^[0-9]{6}$/ ``` 接下来,我们可以使用 PHP 的 $_POST 变量获取用户输入的数据。例如: ``` $email = $_POST['email']; $password = $_POST['password']; $confirm_password = $_POST['confirm_password']; $phone_number = $_POST['phone_number']; $verification_code = $_POST['verification_code']; ``` 然后,我们可以使用 preg_match() 函数来验证用户输入的数据是否符合上述正则表达式。例如: ``` if (!preg_match("/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/", $email)) { echo "Invalid email address"; } if (!preg_match("/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/", $password)) { echo "Invalid password"; } if ($password != $confirm_password) { echo "Passwords do not match"; } if (!preg_match("/^1[3456789]\d{9}$/", $phone_number)) { echo "Invalid phone number"; } if (!preg_match("/^[0-9]{6}$/", $verification_code)) { echo "Invalid verification code"; } ``` 最后,我们可以将用户输入的数据插入到数据库中,完成注册功能。这里我们可以使用 PHP 的 MySQLi 扩展来操作 MySQL 数据库。以下是一个示例: ``` // 连接数据库 $mysqli = new mysqli("localhost", "username", "password", "database"); // 检查连接是否成功 if ($mysqli->connect_error) { die("Connection failed: " . $mysqli->connect_error); } // 插入数据到用户表 $sql = "INSERT INTO users (email, password, phone_number) VALUES ('$email', '$password', '$phone_number')"; if ($mysqli->query($sql) === TRUE) { echo "Registration successful"; } else { echo "Error: " . $sql . "<br>" . $mysqli->error; } // 关闭数据库连接 $mysqli->close(); ``` 希望这个回答能够帮到你。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值