title: Java Regex to mask alphanumeric string and show last 4 digits
body: <p>I have an input string that looks like any of the following:</p>
  
  <ul>
  <li>Z43524429 </li>
  <li>46D92S429</li>
  <li>3488DFJ33</li>
  </ul>
  
  <p>Basically the string can contain alphabet characters or digits. However it cannot contain symbols, just letters and numbers. I would like to mask it so that it looks like this:</p>
  
  <ul>
  <li>*****4429 </li>
  <li>*****S429</li>
  <li>*****FJ33</li>
  </ul>
  
  <p>I've looked everywhere to find an java code example that uses regex to mask this. I've found <a href="https://stackoverflow.com/questions/27545202/masking-last-4-digits-in-javascript">this post</a> on stack but that assumes that the input is purely a number. </p>
  
  <p>I adjusted the regex to <code>/\w(?=\w{4})/g</code> to include characters as well. It seems to work <a href="https://regex101.com/r/jO9aU1/1" rel="nofollow noreferrer">here</a>. But when I try to implement it in java it doesn't work. Here's the line in my java code:</p>
  
  <pre><code>String mask = accountNumber.replace("\\w(?=\\w{4})", "*");
  </code></pre>
  
  <p>The mask ends up being the same as the accountNumber. So obviously the regex is not working. Any thoughts?</p>