자바 정규식 마스킹처리

by 조쉬 posted Jun 26, 2018
?

단축키

Prev이전 문서

Next다음 문서

ESC닫기

크게 작게 위로 아래로 댓글로 가기 인쇄
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class test {
 
    public static void main(String[] args) {
         
        System.out.println(phoneNum("01012345678"));
        System.out.println(name("왕효상"));
        System.out.println(email("dress07@test.com"));
        System.out.println(ip("127.0.0.1"));
         
    }
     
    public static String phoneNum(String str) {
        String replaceString = str;
         
        Matcher matcher = Pattern.compile("^(\\d{3})-?(\\d{3,4})-?(\\d{4})$").matcher(str);
     
        if(matcher.matches()) {
            replaceString = "";
             
            boolean isHyphen = false;
            if(str.indexOf("-") > -1) {
                isHyphen = true;
            }
             
            for(int i=1;i<=matcher.groupCount();i++) {
                String replaceTarget = matcher.group(i);
                if(i == 2) {
                    char[] c = new char[replaceTarget.length()];
                    Arrays.fill(c, '*');
                     
                    replaceString = replaceString + String.valueOf(c); 
                } else {
                    replaceString = replaceString + replaceTarget;
                }
                 
                if(isHyphen && i < matcher.groupCount()) {
                    replaceString = replaceString + "-";
                }
            }
        }
         
        return replaceString;
    }
     
    public static String name(String str) {
        String replaceString = str;
         
        String pattern = "";
        if(str.length() == 2) {
            pattern = "^(.)(.+)$";
        } else {
            pattern = "^(.)(.+)(.)$";
        }
         
        Matcher matcher = Pattern.compile(pattern).matcher(str);
     
        if(matcher.matches()) {
            replaceString = "";
             
            for(int i=1;i<=matcher.groupCount();i++) {
                String replaceTarget = matcher.group(i);
                if(i == 2) {
                    char[] c = new char[replaceTarget.length()];
                    Arrays.fill(c, '*');
                     
                    replaceString = replaceString + String.valueOf(c);
                } else {
                    replaceString = replaceString + replaceTarget;
                }
                 
            }
        }
         
        return replaceString;
    }
     
    public static String email(String str) {
        String replaceString = str;
         
        Matcher matcher = Pattern.compile("^(..)(.*)([@]{1})(.*)$").matcher(str);
         
        if(matcher.matches()) {
            replaceString = "";
             
            for(int i=1;i<=matcher.groupCount();i++) {
                String replaceTarget = matcher.group(i);
                if(i == 2) {
                    char[] c = new char[replaceTarget.length()];
                    Arrays.fill(c, '*');
                     
                    replaceString = replaceString + String.valueOf(c);
                } else {
                    replaceString = replaceString + replaceTarget;
                }
            }
             
        }
         
        return replaceString;
    }
     
    public static String ip(String str) {
        String replaceString = str;
         
        Matcher matcher = Pattern.compile("^([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})\\.([0-9]{1,3})$").matcher(str);
         
        if(matcher.matches()) {
            replaceString = "";
             
            for(int i=1;i<=matcher.groupCount();i++) {
                String replaceTarget = matcher.group(i);
                if(i == 3) {
                    char[] c = new char[replaceTarget.length()];
                    Arrays.fill(c, '*');
                     
                    replaceString = replaceString + String.valueOf(c);
                } else {
                    replaceString = replaceString + replaceTarget;
                }
                if(i < matcher.groupCount()) {
                    replaceString =replaceString + ".";
                }
            }
        }
         
        return replaceString;
    }
 
}

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5