본문 바로가기

프로그래밍/Etc.

C# 카드번호 여부 체크 로직

카드번호 패턴 분석 로직


요즘 개인정보 보호로 인해 TEST 서버 로그처리 부분에서 카드번호 , 전화번호 , 이메일 등이 들어가는 경우가 있는데 이를 *** 처리하는 로직을 구현하였습니다. 


그중에서 특정 텍스트에서 카드번호를 찾아서 *** 처리하는 로직을 구현하였습니다.


Text 예시 구문

1301029250010712T00000125149804102  M010711T000001251496    000000010

030000019400CJ1401070001     N 0500 @0100001234   01000000000009100000100011037125138364    카드번호찾기 999101741T0000001251539 1301029250010712

M010711T000001251496     000000010

여기서 카드번호로 예상되는 숫자를 찾아서 끝에 3자리를 *** 처리 

여기서 말하는 카드 번호는 14~ 17자리로 이루어진 숫자를 뜻함.

해당 구문에서 14~17자리로 된 카드번호를 정규식으로 변환하는 로직인데 문자 가운데 특수 문자가 있을 수 있는 구문이므로 특수문자를 체크해서 특수문자를 제외한 카드번호를 찾을수 있도록 구현 


카드 번호 변환 정규식

string cardPatten = "^([0-9]{12})([0-9]{3,4})$";

value = (new Regex(cardPatten)).Replace(value, "$1****");


휴대폰 변환 정규식

string phonePatten = "^(01[016789][0-9]{4})([0-9]{3,4})$";

value = (new Regex(phonePatten)).Replace(value, "$1****");


이메일 변환 정규식 

string emailPatten = "([^@]){1}([^@]*@)([^\\.]*)\\.(.*)";

value = (new Regex(emailPatten)).Replace(value, "$1***@$3.$4");


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
static void Main(string[] args) {
       
        string value = "위의 푸른색 TextBox 내용";
        string[] result = value.Split(' ');
        string resultString = "";
        string cardPatten = "^([0-9]{12})([0-9]{3,4})$";
        
        for (int i = 0; i < result.Length; i++) {
            //카드번호
            if (result[i].Length > 14) {
                if (result[i].Length < 18) {
                    result[i] = (new Regex(cardPatten)).Replace(result[i], "$1****");
                    resultString += result[i] + " ";
                } else {
                    int cardnumber = 0;
                    // 문자 체크 해서 파싱 
                    if (!int.TryParse(result[i], out cardnumber)) {
                        char[] card = result[i].ToCharArray();
                        int strcount = 1;
                        string strchar = string.Empty;
                        for (int j = 0; j < card.Length; j++) {
                            //숫자가 아닌 문자열이 오면 파싱
                            if (!int.TryParse(card[j].ToString(), out cardnumber)) {
                                strchar += j + ",";
                                strcount++;
                            }
                        }
                        string[] number = new string[strcount];
                        for (int z = 0; z < strcount; z++) {
                            number[z] = strchar.Split(',')[z];
                            //
                            if (z == 0) {
                                if (number[z] == "") {
                                    if (result[i].Length > 17) {
                                        resultString += result[i] + " ";
                                    }
                                } else {
                                    if (result[i].Substring(0int.Parse(number[z])).Length > 14 && result[i].Substring(0int.Parse(number[z])).Length < 18) {
                                        string test3 = (new Regex(cardPatten)).Replace(result[i].Substring(0int.Parse(number[z])), "$1****");
                                        resultString += test3 + result[i].Substring(int.Parse(number[z]), 1);
                                    } else if (int.Parse(number[z]) == 0)
                                        resultString += result[i].Substring(int.Parse(number[z]), 1);
                                    else
                                        resultString += result[i].Substring(0int.Parse(number[z])) + result[i].Substring(int.Parse(number[z]), 1);
                                }
                            } else {
                                if (number[z] == "") {
                                    resultString += result[i].Substring((int.Parse(number[z - 1]) + 1), result[i].Length - int.Parse(number[z - 1]) - 1+ " ";
                                } else {
                                    if (result[i].Substring((int.Parse(number[z - 1]) + 1), result[i].Length - int.Parse(number[z - 1]) - 1).Length > 14 && result[i].Substring((int.Parse(number[z - 1]) + 1), result[i].Length - int.Parse(number[z - 1]) - 1).Length < 18) {
                                        string EndMark = (new Regex(cardPatten)).Replace((result[i].Substring((int.Parse(number[z - 1]) + 1), result[i].Length - int.Parse(number[z - 1]) - 1)), "$1****");
                                        resultString += EndMark;
                                    } else if ((int.Parse(number[z - 1]) + 1 == int.Parse(number[z]))) {
                                        resultString += result[i].Substring((int.Parse(number[z - 1]) + 1), 1);
                                    }
                                }
                            }
                        }
                    }
                }
            }
           
            else {
                //14자리보다 작은 숫자 패스 
                resultString += result[i] + " ";
            }
        }
        Console.WriteLine(resultString);


cs


변환 결과

이 방법보다 더 심플한 방법이 있을듯 합니다. 

좋은 의견있으면 부탁드립니다.