I have a method for the phone number masking. I need to replace all digits with stars except the last 4. Sample inputs would be: +91 (333) 444-5678 and +1(333) 456-7890. Outputs should look this way:
But my output actually looks like this:
So here is my code:
public static String maskPhoneNumber(String inputPhoneNum){
return inputPhoneNum.replaceAll("\\(", "-")
.replaceAll("\\)", "-")
.replaceAll(" ", "-")
.replaceAll("\\d(?=(?:\\D*\\d){4})", "*");
}
My method works with different number of digits in country codes, but it breaks in cases when instead of a space between digits there are brackets near the country code (triad after it). I would be grateful for some hints on how I can improve my approach!

