Post List

태그

2019년 1월 24일 목요일

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

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);
   }
   
   
}



댓글 없음:

댓글 쓰기