正则表达式在 java 中屏蔽 PAN 号

Regex to mask PAN number in java

我需要屏蔽 PAN(永久帐号)。

它的前 5 个字符代表从 AAA 到 ZZZ 的字母序列 运行。接下来的 4 个字符是从 0001 到 9999 的连续数字 运行。第十个字符是字母。

例如:ABCDE1234F

需要用 # 标记第 3、4、5、7、10 个字符,例如: AB###1#23# 使用正则表达式。

我想你想要这样的东西:

"1234567890".replaceAll("(.{2})(.{3})(.{1})(.{1})(.{2})(.{1})", "#####")

这显然不是你要找的,但我还是要把它扔掉。

在我看来,这种事情的正则表达式对于一种类型的字符串来说太具体了。对于要以不同格式屏蔽的每种不同类型的字符串,您将需要不同的正则表达式。对于需要不同屏蔽格式的不同类型的字符串,它只是不够灵活。我想说的是,我认为将字符串连同您想要的屏蔽格式一起传递到方法中并返回所需的屏蔽字符串会更容易,例如:

String pan = "ABCDE1234F";
String newPAN = maskString(pan, 0, "??###?#??#");
System.out.println(newPAN);

// Displays in Console:  AB###1#34#

或者类似的东西:

String pan = "Account Number: ABCDE1234F";
String newPAN = maskString(pan, 16, "??###?#??#");
System.out.println(newPAN);

// Displays in Console:  Account Number: AB###1#34#

或者类似的东西:

String pan = "Account Number: ABCDE1234F (Listed).";
String newPAN = maskString(pan, pan.indexOf("ABC"), "??###?#??#");
System.out.println(newPAN);

// Displays in Console:  Account Number: AB###1#34# (Listed).

下面的方法是这样做的:

/**
 * This method allows you to mask a string in whichever format you like by
 * simply supplying a format string.<br><br>
 * <p>
 * Once the input string is masked, it can not be Unmasked. Your code will
 * need to keep track of what the original string was if this is a
 * requirement.<br><br>
 *
 * 
 * @param inputString (string) The string to mask.<br>
 * 
 * @param startIndex  (int) Normally you would most likely use this method<pre>
 *            on a single string entity and therefore the Start
 *            Index would be 0, at the first character of the
 *            string. This however may not always be the case. There
 *            can be times when you want to mask a substring within
 *            the supplied input string located at a specific index.
 *            You can supply that specific index value here.</pre>
 *
 * @param maskFormat  (Optional - String) Default (if nothing is supplied)<pre>
 *            is to mask all characters in string as Asterisks (*).
 *            The question mark character (?) is used within the
 *            format string to indicate that an original string
 *            character is to take that character position. Any other
 *            characters are considered mask characters, for example, 
 *            if we have a phone number string like:
 * 
 *                        <b>"624-344-1212"</b>
 * 
 *            and we want to mask all numbers with asterisks (*) but keep 
 *            the dashes as they are then we would use a format string that 
 *            looks like this: 
 * 
 *                         <b>"***?***?****"</b>
 * 
 *            The returned string would contain: 
 *  
 *                         <b>"***-***-****"</b>
 * 
 *            Since the Question mark character has special meaning within 
 *            this method's format string, you would need to escape (\) that 
 *            character if you wanted to use it as a mask character, for 
 *            example:
 * 
 *                     <b>"\?\?\??\?\?\??\?\?\?\?"</b>
 * 
 *            will tell this method to return the following string:
 * 
 *                           <b>"???-???-????"</b></pre>
 *
 * @return (String) The masked String.
 * 
 */
public static String maskString(String inputString, int startIndex, String... maskFormat) {
    String startString = "";
    if (startIndex > 0) {
        startString = inputString.substring(0, startIndex);
        inputString = inputString.substring(startIndex);
    }

    String maskFmt = inputString.replaceAll(".*", "*");
    if (maskFormat.length > 0) {
        if (!maskFormat[0].isEmpty()) {
            /* ASCII 22 is used to replace escaped Question marks so 
               that the regular Question marks can be processed properly. */
            maskFormat[0] = maskFormat[0].replace("\?", Character.toString((char) 22));
            maskFmt = maskFormat[0];
        }
    }
    if (inputString.length() > maskFmt.length()) {
        for (int i = maskFmt.length(); i < inputString.length(); i++) {
            maskFmt += "?";
        }
    }
    else if (inputString.length() < maskFmt.length()) {
        maskFmt = maskFmt.substring(0, maskFmt.length()
                - (maskFmt.length() - inputString.length()));
    }

    String maskedString = "";
    for (int i = 0; i < maskFmt.length(); i++) {
        maskedString += maskFmt.substring(i, i + 1).equals("?")
                ? inputString.substring(i, i + 1) : maskFmt.substring(i, i + 1);
    }
    if (!startString.isEmpty()) {
        maskedString = startString + maskedString;
    }
    /* Return the masked String but first convert any ASCII 22 (which 
       would be escaped Question marks if any) characters to Question 
       mark characters. */
    return maskedString.replace(Character.toString((char) 22), "?");
}