I need to mask PAN (Permanent account number).
Its first 5 characters represent the alphabetic series running from AAA to ZZZ. Next 4 characters are sequential numbers running from 0001 to 9999. Tenth character is an alphabetic.
eg: ABCDE1234F
Need to mast 3,4,5,7,10 th character's with # eg: AB###1#23# using regex.
CodePudding user response:
I think you want something like this:
"1234567890".replaceAll("(.{2})(.{3})(.{1})(.{1})(.{2})(.{1})", "$1###$3#$5#")
CodePudding user response:
This is obviously not what you're looking for but I'm going to throw it out there anyways.
In my opinion, RegEx for this sort of thing is far too specific for one type of string. For every different type of string to be masked in a different format you will need a different Regular Expression. It's just not flexible enough for different types of strings where each requires a different masking format. What I'm trying to say is, I think it's just easier to pass the string into a method along with the masking format you want and the desired masked string is returned, for example:
String pan = "ABCDE1234F";
String newPAN = maskString(pan, 0, "??###?#??#");
System.out.println(newPAN);
// Displays in Console: AB###1#34#
Or perhaps something like:
String pan = "Account Number: ABCDE1234F";
String newPAN = maskString(pan, 16, "??###?#??#");
System.out.println(newPAN);
// Displays in Console: Account Number: AB###1#34#
Or perhaps something like:
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).
The method below does this:
/**
* 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), "?");
}