java使用正则表达式对注册页面进行验证

 1 package regex;
 2 
 3 import java.util.Scanner;
 4 import java.util.regex.Matcher;
 5 import java.util.regex.Pattern;
 6 
 7 public class registered {
 8 
 9     public static void main(String[] args) {
10         //注册用户
11         Scanner sc=new Scanner(System.in);
12         System.out.println("请输入用户名:");
13         String uname=sc.next();
14         System.out.println("请输入密码:");
15         String passwd=sc.next();
16         System.out.println("请输入确认密码:");
17         String repasswd=sc.next();
18         
19 /*        String uname="wangheng";
20         String passwd="222assAS123";
21         String repasswd="432Pass123";*/        
22                 
23         boolean b=uname.matches("\\w{3,10}");                    //方法一
24         if(b==true){
25         Pattern p0=Pattern.compile(".{6,12}");//长度6到12个
26         Pattern p1=Pattern.compile(".*[A-Z]+");//
27         Pattern p2=Pattern.compile(".*[a-z]+");
28         Pattern p3=Pattern.compile(".*\\d+");
29         Matcher m0=p0.matcher(passwd);
30         Matcher m1=p1.matcher(passwd);
31         Matcher m2=p2.matcher(passwd);
32         Matcher m3=p3.matcher(passwd);
33         if(m0.lookingAt()==true&&
34            m1.lookingAt()==true&&        
35            m2.lookingAt()==true&&
36            m3.lookingAt()==true){
37                 boolean b2=passwd.matches(repasswd);
38                 if(b2){
39                     System.out.println("注册成功!");
40                 }else{
41                     System.out.println("确认密码与密码不同!");
42                 }
43         }else{
44                 System.out.println("密码输入错误!");
45             }
46         }else{
47             System.out.println("用户名输入错误!");
48         }
49         
50         //方法二
51         Pattern p1=Pattern.compile("[A-Z]+");
52         Pattern p2=Pattern.compile("[a-z]+");
53         Pattern p3=Pattern.compile("\\d+");
54         Matcher m1=p1.matcher(passwd);
55         Matcher m2=p2.matcher(passwd);
56         Matcher m3=p3.matcher(passwd);
57         if(uname.matches("\\w{3,10}")&&passwd.matches(".{6,12}")&&m1.find()&&m2.find()&&m3.find()){
58             System.out.println("注册成功!");
59         }else{
60             System.out.println("注册失败!");
61         }
62        

          //方法三

       String regex="^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{6,15}$";
           System.out.println(passwd.matches(regex));

63     }
64 }

 

转载于:https://www.cnblogs.com/1020182600HENG/p/5909346.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值