title: Replace group 1 of Java regex with out replacing the entire regex
body: <p>I have a regex pattern that will have only one group. I need to find texts in the input strings that follows the pattern and replace ONLY the match group 1. For example I have the regex pattern and the string to be applied on as shown below. The replacement string is "&lt;---->"</p>
  
  <pre><code>Pattern p = Pattern.compile("\\w*(lan)\\w+");
  Matcher m = p.matcher("plan plans lander planitia");
  </code></pre>
  
  <p>The expected result is </p>
  
  <pre><code>plan p&lt;---&gt;s &lt;---&gt;der p&lt;---&gt;itia
  </code></pre>
  
  <p>I tried following approaches</p>
  
  <pre><code>    String test = "plan plans lander planitia";
      Pattern p = Pattern.compile("\\w*(lan)\\w+");
      Matcher m = p.matcher(test);
      String result = "";
      while(m.find()){
          result = test.replaceAll(m.group(1),"&lt;---&gt;");
      }
      System.out.print(result);
  </code></pre>
  
  <p>This gives result as </p>
  
  <pre><code>p&lt;---&gt; p&lt;---&gt;s &lt;---&gt;der p&lt;---&gt;itia
  </code></pre>
  
  <p>Another approach</p>
  
  <pre><code>    String test = "plan plans lander planitia";
      Pattern p = Pattern.compile("\\w*(lan)\\w+");
      Matcher m = p.matcher(test);
      String result = "";
      while(m.find()){
          result = test.replaceAll("\\w*(lan)\\w+","&lt;---&gt;");
      }
      System.out.print(result);
  </code></pre>
  
  <p>Result is</p>
  
  <pre><code>plan &lt;---&gt; &lt;---&gt; &lt;---&gt;
  </code></pre>
  
  <p>I have gone through <a href="https://stackoverflow.com/questions/2921212/replace-capturing-group">this</a> link. Here the part of the string before the match is always constant and is "foo" but in my case it varies. Also I have looked at <a href="https://stackoverflow.com/questions/1277157/java-regex-replace-with-capturing-group">this</a> and <a href="https://stackoverflow.com/questions/988655/can-i-replace-groups-in-java-regex">this</a> but I am unable to apply any on the solutions given to my present scenario.</p>
  
  <p>Any help is appreciated</p>