我需要屏蔽PAN(永久帐号)。
它的前5个字符代表从AAA到ZZZ的字母序列。接下来的4个字符是从0001到9999的连续数字。第10个字符为字母
如:ABCDE1234F
需要用# eg标记第3,4,5,7,10个字符:AB###1#23# using regex.
我想你需要这样的东西:
"1234567890".replaceAll("(.{2})(.{3})(.{1})(.{1})(.{2})(.{1})", "$1###$3#$5#")
这显然不是你想要的,但我还是要把它扔在那里。
在我看来,RegEx对于这种类型的字符串来说太特定了。对于每种不同类型的字符串以不同的格式进行屏蔽,您将需要不同的正则表达式。对于不同类型的字符串来说,它不够灵活,因为每种类型都需要不同的屏蔽格式。我想说的是,我认为把字符串和你想要的屏蔽格式一起传递给一个方法会更容易,然后返回想要的屏蔽字符串,例如:
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), "?");
}