Java练习_day09_正则表达式的实例

/**
 * 3、 定义一个用户类,提供一个注册方法,
 * 注册可以用邮箱注册或者手机号注册,判断用户是否输入正确账号信息,正确返回 true ,
 * 错误返回false(百度生成正则表达式)
 */
public class User {

    private String name;
    private String email;
    private String phone;


    public static Boolean isEmail(String str){
       Boolean result=str.matches("\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}");
       return result;
    }


    public static Boolean isPhone(String str){
        Boolean result=str.matches("0?(13|14|15|18)[0-9]{9}");
        return result;
    }


    //get set方法
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}
/**
 * 功能描述:测试类
 *
 * @author 顾英明
 * @date 2022/12/05  22:27
 */

public class Test {
    public  static final String PHONEError="手机号输入错误";
    public static final String EMAILERROR="邮箱输入错误";
    public static final String INPUTERROR="输入错误,请输入1或0来选择注册的方式";
    public static final String SUCCESS="注册成功";

    public static void main(String[] args) {

        User user=new User();
        //当用户输入1时用邮箱注册 ,输入0时用手机号注册
        String str="1";
        Boolean isSuccess=false;


        for (int i = 0; i <=3 ; i++) {
            user.setName("张三");

            //通过手机号注册
            if (str.equals("1")){
                String myPhone="13912572979";
                if (User.isPhone(myPhone)){
                    user.setPhone(myPhone);
                    isSuccess=true;
                }
                else{
                    System.out.println(PHONEError);
                    isSuccess =false;
                }
                //通过邮箱注册
            }else if(str.matches("0")){
                String myEmail="2919925034@qq.com";
                if (User.isEmail(myEmail)){
                    user.setEmail(myEmail);
                    isSuccess=true;
                }else {
                    System.out.println(EMAILERROR);
                    isSuccess=false;
                }

            }else {
                System.out.println(INPUTERROR);
                isSuccess=false;
            }

        }

        if (isSuccess){
            System.out.println(SUCCESS);
            System.out.println(user.toString());
        }

    }
}

运行结果:


注册成功
User{name='张三', email='null', phone='13912572979'}
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

顾英明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值