title: Replace emails in string in Java
body: <p>I need to protect the email addresses contained in a text. Ideally find a regular expression that could do it more effectively.</p>
  
  <p>Example:</p>
  
  <pre>
  Hi:
  
  My Name is Alex and my mail is <strong>alexmail@domain.com</strong> but you can reply to
  <strong>alexreply@other.domain.com</strong>.
  </pre>
  
  <p>Desired output:</p>
  
  <pre>
  Hi:
  
  My Name is Alex and my mail is <strong>ale****@domain.com</strong> but you can reply to
  <strong>ale****@other.domain.com</strong>.
  </pre>
  
  <p>The logic is: keep first 3 characters and replace the rest with * until the @.</p>
  
  <pre><code>a@mail.com     =&gt; a****@mail.com
  ab@mail.com    =&gt; ab****@mail.com
  abc@mail.com   =&gt; abc****@mail.com
  abcd@mail.com  =&gt; abc****@mail.com
  abcde@mail.com =&gt; abc****@mail.com
  </code></pre>
  
  <p>Now, I made a function to protect a mail in this way, but when it is a text containing several emails then I can not use <em>replaceAll</em>.</p>
  
  <pre><code>public static String protectEmailAddress(String emailAddress) {
       String[] split = emailAddress.split("@");
       if (split[0].length() &gt;= 3) {
           split[0] = split[0].substring(0, 3);  
       }
       emailAddress = StringUtils.join(split, "****@");
  
       return emailAddress;
  }
  </code></pre>
  
  <p>So basically what I need is a nice regex that work. Something similar to <a href="https://stackoverflow.com/questions/16071421/regex-to-replace-email-address-domains">this</a> but with another section of the mail, if possible.</p>
  
  <p>Thanks...</p>