title: Java RegEx: Replace part of source string
body: <p>If the source string contains the pattern, then replace it with something or remove it. One way to do it is to do something like this</p>
  
  <pre><code>Pattern p = Pattern.compile(regex);
  Matcher m = p.matcher(sourceString);
  while(m.find()){
    String subStr = m.group().replaceAll('something',""); // remove the pattern sequence
    String strPart1 = sourceString.subString(0,m.start());
    String strPart2 = sourceString.subString(m.start()+1);
    String resultingStr = strPart1+subStr+strPart2;
    p.matcher(...);
  }
  </code></pre>
  
  <p>But I want something like this</p>
  
  <pre><code>Pattern p = Pattern.compile(regex);
      Matcher m = p.matcher(sourceString);
      while(m.find()){
        m.group.replaceAll(...);// change the group and it is source string is automatically updated    
  }
  </code></pre>
  
  <p>Is this possible?</p>
  
  <p>Thanks</p>