0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

javaでマスク処理

Last updated at Posted at 2024-10-22

郵便番号マスキング

public class ZipCodeMasker {
    public static void main(String[] args) {
        String zipCode = "123ー456";
        String maskedZipCode = maskZipCode(zipCode);
        System.out.println(maskedZipCode); // 期待値:***ー***
    }

    public static String maskZipCode(String zipCode) {
        if (zipCode == null || zipCode.isEmpty()) {
            return zipCode;
        }

        StringBuilder masked = new StringBuilder();
        for (char c : zipCode.toCharArray()) {
            if (c == 'ー') {
                masked.append(c);
            } else {
                masked.append('*');
            }
        }
        return masked.toString();
    }
}

電話番号マスキング

public class PhoneNumberMasker {
    public static void main(String[] args) {
        // テストケース
        System.out.println(maskPhoneNumber("123ー456ー789")); // 期待値: ***ー***ー789
        System.out.println(maskPhoneNumber("123456789"));     // 期待値: *****6789
    }

    public static String maskPhoneNumber(String phoneNumber) {
        // ハイフン(ー)が含まれている場合の処理
        if (phoneNumber.contains("ー")) {
            String[] parts = phoneNumber.split("ー");
            StringBuilder maskedNumber = new StringBuilder();

            for (int i = 0; i < parts.length; i++) {
                if (i != 0) {
                    maskedNumber.append("ー");
                }
                if (i == parts.length - 1) {
                    // 最後の部分はマスクしない
                    maskedNumber.append(parts[i]);
                } else {
                    // その他の部分は全てマスク
                    maskedNumber.append(parts[i].replaceAll(".", "*"));
                }
            }
            return maskedNumber.toString();
        } else {
            // ハイフンが含まれていない場合の処理
            int length = phoneNumber.length();
            if (length <= 4) {
                return phoneNumber; // 4桁以下の場合、マスク不要
            }
            String maskedPart = phoneNumber.substring(0, length - 4).replaceAll(".", "*");
            String unmaskedPart = phoneNumber.substring(length - 4);
            return maskedPart + unmaskedPart;
        }
    }
}

住所マスキング

public String maskAddress(String address) {
        // 修正された正規表現パターン(全角数字 + ('ー' + 全角数字)の繰り返し)
        String pattern = "[0-9]+(?:ー[0-9]+)+";
        
        // パターンにマッチするか確認
        if (address.matches(".*" + pattern + ".*")) {
            // マッチする場合、マスキング処理を実施
            StringBuilder maskedAddress = new StringBuilder();
            // パターンの位置を特定
            java.util.regex.Pattern regex = java.util.regex.Pattern.compile(pattern);
            java.util.regex.Matcher matcher = regex.matcher(address);
            boolean[] keepChar = new boolean[address.length()];
            while (matcher.find()) {
                for (int i = matcher.start(); i < matcher.end(); i++) {
                    keepChar[i] = true; // マッチ部分は保持
                }
            }
            // 文字ごとにマスク処理
            for (int i = 0; i < address.length(); i++) {
                char c = address.charAt(i);
                if (c == ' ' || keepChar[i]) { // 全角スペースまたは保持する文字
                    maskedAddress.append(c);
                } else {
                    maskedAddress.append('*'); // マスク
                }
            }
            return maskedAddress.toString();
        } else {
            // マッチしない場合、住所の下5文字以外をマスク
            int unmaskLength = 5;
            if (address.length() <= unmaskLength) {
                return address; // 住所が短い場合はそのまま返す
            } else {
                StringBuilder maskedAddress = new StringBuilder();
                for (int i = 0; i < address.length() - unmaskLength; i++) {
                    maskedAddress.append('*'); // マスク
                }
                maskedAddress.append(address.substring(address.length() - unmaskLength));
                return maskedAddress.toString();
            }
        }
    }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?