Post List

태그

2019년 1월 24일 목요일

자바 정규식(비밀번호, 이메일, 아이피주소)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.regex.Matcher;
import java.util.regex.Pattern;
  
public class RegexTest1{
  
   private Pattern pattern;
   private Matcher matcher;
 
   public RegexTest1(String pattern){
    this.pattern = Pattern.compile(pattern);
   }
    
   public boolean validate(String text){
     
    matcher = pattern.matcher(text);
    return matcher.matches();
           
   }
    
   public static void main(String [] args){
     
    String passwordPattern = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})";
     
    String emailPattern = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
     + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
 
    String ipaddressPattern = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
               + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
     
     
    RegexTest1 password = new RegexTest1(passwordPattern);
    RegexTest1 email = new RegexTest1(emailPattern);
    RegexTest1 ipaddress = new RegexTest1(ipaddressPattern);
     
    String text1 = "aaSS12!@";
    String text2 = "ASsdv221@nate.com";
    String text3 = "127.1.1.1";
     
    System.out.println(password.validate(text1) + "\t" + text1);
    System.out.println(email.validate(text2) + "\t" + text2);
    System.out.println(ipaddress.validate(text3) + "\t" + text3);
     
    text1 = "aaS!@";
    text2 = "ASsdv221@natecom";
    text3 = "0127.1.1.1";
     
    System.out.println(password.validate(text1) + "\t" + text1);
    System.out.println(email.validate(text2) + "\t" + text2);
    System.out.println(ipaddress.validate(text3) + "\t" + text3);
   }
    
    
}

댓글 없음:

댓글 쓰기