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?

マスキング

Posted at
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AddressMasking {

    public static String maskAddress(String address) {
        if (address == null || address.isEmpty()) return address;

        // 正規表現: 数字−数字 をすべて検出(Unicodeの全角ハイフン)
        Pattern pattern = Pattern.compile("\\d\u2212\\d+");
        Matcher matcher = pattern.matcher(address);

        // 残すべき範囲を記録
        boolean[] unmaskFlags = new boolean[address.length()];

        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            for (int i = start; i < end; i++) {
                unmaskFlags[i] = true;
            }
        }

        // マスキング処理
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < address.length(); i++) {
            if (unmaskFlags[i]) {
                result.append(address.charAt(i));
            } else {
                result.append('*');
            }
        }

        return result.toString();
    }

    // テスト
    public static void main(String[] args) {
        String input = "東京都 北区 赤羽2−15−7 赤羽ビル11−102";
        System.out.println(maskAddress(input));
        // 出力例(わかりやすく空白表示)
        // ***********2−15−7******11−102
    }
}
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?