title: Java - regex to replace FIRST and LAST character with another one in ONE SHOT
body: <p>Consider i have this String here :</p>
  
  <pre><code>[Hel[don't change in the middle]lo]
  </code></pre>
  
  <p>How can i replace the first and last bracket with another character like this :</p>
  
  <pre><code>#Hel[don't change in the middle]lo#
  </code></pre>
  
  <p>I know this is possible like this :</p>
  
  <pre><code>String str = "[Hel[don't change in the middle]lo]";
  str = str.replaceFirst("\\[", "#");
  if (str.substring(str.length() - 1).equals("]")) {
      str = str.substring(0, str.length() - 1) + "#";
      System.out.println(str);
  }
  </code></pre>
  
  <blockquote>
    <p>Is there any other simple way using regex, so i can replace the first
    and the last one in one shot?</p>
  </blockquote>
  
  <p>like this :</p>
  
  <pre><code>str = str.replace("myregex");
  </code></pre>