title: Fastest way to replace last occurrence of pattern in a string
body: <p>After some looking around at different questions about replacing the last word, I didn't find one that covered the <strong>fastest way</strong>. In my scenario I want to replace the last bit of a string after a period, or the last word and everything after.</p>
  
  <p>To better explain, this method replaces the string exactly how I want it: (Edit: I changed the regex based on Avinash's response, still looking for a faster alternative as the regex replaceall is still slow)</p>
  
  <pre><code>return permission.replaceAll("\\w+(?:\\.\\*)?$", "*");
  </code></pre>
  
  <p>Example in -> out</p>
  
  <pre><code>test.test.test -&gt; test.test.*
  test.test.* -&gt; test.*
  </code></pre>
  
  <p>While this works, I am looking for a faster way to achieve the same results. This method is called often and it has appeared at the top of my profiling cpu results.</p>